Skip to content

Commit

Permalink
Merge pull request #198 from kimzeuner/Alert-features
Browse files Browse the repository at this point in the history
Alert Features
  • Loading branch information
jm-73 committed Sep 24, 2023
2 parents deb2006 + 8b800d0 commit 2a9ae2f
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 1 deletion.
184 changes: 184 additions & 0 deletions custom_components/indego/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,21 @@
CONF_ATTR,
CONF_SEND_COMMAND,
CONF_SMARTMOWING,
CONF_DELETE_ALERT,
CONF_READ_ALERT,
DEFAULT_NAME_COMMANDS,
DOMAIN,
ENTITY_ALERT,
ENTITY_ALERT_COUNT,
ENTITY_ALERT_ID,
ENTITY_ALERT_ERROR_CODE,
ENTITY_ALERT_HEADLINE,
ENTITY_ALERT_DATE,
ENTITY_ALERT_MESSAGE,
ENTITY_ALERT_READ_STATUS,
ENTITY_ALERT_FLAG,
ENTITY_ALERT_PUSH,
ENTITY_ALERT_DESCRIPTION,
ENTITY_BATTERY,
ENTITY_LAST_COMPLETED,
ENTITY_LAWN_MOWED,
Expand All @@ -67,6 +79,10 @@
SENSOR_TYPE,
SERVICE_NAME_COMMAND,
SERVICE_NAME_SMARTMOW,
SERVICE_NAME_DELETE_ALERT,
SERVICE_NAME_READ_ALERT,
SERVICE_NAME_DELETE_ALERT_ALL,
SERVICE_NAME_READ_ALERT_ALL,
)
from .sensor import IndegoSensor

Expand All @@ -82,6 +98,22 @@
vol.Required(CONF_SMARTMOWING): cv.string
})

SERVICE_SCHEMA_DELETE_ALERT = vol.Schema({
vol.Required(CONF_DELETE_ALERT): cv.positive_int
})

SERVICE_SCHEMA_READ_ALERT = vol.Schema({
vol.Required(CONF_READ_ALERT): cv.positive_int
})

SERVICE_SCHEMA_DELETE_ALERT_ALL = vol.Schema({
vol.Required(CONF_DELETE_ALERT): cv.string
})

SERVICE_SCHEMA_READ_ALERT_ALL = vol.Schema({
vol.Required(CONF_READ_ALERT): cv.string
})


def FUNC_ICON_MOWER_ALERT(state):
if state:
Expand Down Expand Up @@ -112,6 +144,85 @@ def FUNC_ICON_MOWER_ALERT(state):
CONF_DEVICE_CLASS: DEVICE_CLASS_PROBLEM,
CONF_ATTR: ["alerts_count"],
},
ENTITY_ALERT_PUSH: {
CONF_TYPE: BINARY_SENSOR_TYPE,
CONF_NAME: "alert push",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_ATTR: [],
},
ENTITY_ALERT_COUNT: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert count",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_ID: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert id",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_ERROR_CODE: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert errorcode",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_HEADLINE: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert headline",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_DATE: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert date",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_MESSAGE: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert message",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_READ_STATUS: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert read status",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_FLAG: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert flag",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_ALERT_DESCRIPTION: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "alert description",
CONF_ICON: "mdi:alert-octagon-outline",
CONF_DEVICE_CLASS: None,
CONF_UNIT_OF_MEASUREMENT: None,
CONF_ATTR: [],
},
ENTITY_MOWER_STATE: {
CONF_TYPE: SENSOR_TYPE,
CONF_NAME: "mower state",
Expand Down Expand Up @@ -262,6 +373,37 @@ async def async_send_smartmowing(call):
_LOGGER.debug("Indego.send_smartmowing service called, set to %s", enable)
await instance._indego_client.put_mow_mode(enable)
await instance._update_generic_data()
async def async_delete_alert(call):
"""Handle the service call."""
enable = call.data.get(CONF_DELETE_ALERT, DEFAULT_NAME_COMMANDS)
_LOGGER.debug("Indego.delete_alert service called, with command: %s", enable)
await hass.data[DOMAIN][entry.entry_id]._update_alerts()
await hass.data[DOMAIN][entry.entry_id]._indego_client.delete_alert(enable)
await hass.data[DOMAIN][entry.entry_id]._update_alerts()

async def async_delete_alert_all(call):
"""Handle the service call."""
enable = call.data.get(CONF_DELETE_ALERT, DEFAULT_NAME_COMMANDS)
_LOGGER.debug("Indego.delete_alert_all service called, with command: %s", "all")
await hass.data[DOMAIN][entry.entry_id]._update_alerts()
await hass.data[DOMAIN][entry.entry_id]._indego_client.delete_all_alerts()
await hass.data[DOMAIN][entry.entry_id]._update_alerts()

async def async_read_alert(call):
"""Handle the service call."""
enable = call.data.get(CONF_READ_ALERT, DEFAULT_NAME_COMMANDS)
_LOGGER.debug("Indego.read_alert service called, with command: %s", enable)
await hass.data[DOMAIN][entry.entry_id]._update_alerts()
await hass.data[DOMAIN][entry.entry_id]._indego_client.put_alert_read(enable)
await hass.data[DOMAIN][entry.entry_id]._update_alerts()

async def async_read_alert_all(call):
"""Handle the service call."""
enable = call.data.get(CONF_READ_ALERT, DEFAULT_NAME_COMMANDS)
_LOGGER.debug("Indego.read_alert_all service called, with command: %s", "all")
await hass.data[DOMAIN][entry.entry_id]._update_alerts()
await hass.data[DOMAIN][entry.entry_id]._indego_client.put_all_alerts_read()
await hass.data[DOMAIN][entry.entry_id]._update_alerts()

# In HASS we can have multiple Indego component instances as long as the mower serial is unique.
# So the mower services should only need to be registered for the first instance.
Expand All @@ -281,6 +423,30 @@ async def async_send_smartmowing(call):
async_send_smartmowing,
schema=SERVICE_SCHEMA_SMARTMOWING,
)
hass.services.async_register(
DOMAIN,
SERVICE_NAME_DELETE_ALERT,
async_delete_alert,
schema=SERVICE_SCHEMA_DELETE_ALERT
)
hass.services.async_register(
DOMAIN,
SERVICE_NAME_READ_ALERT,
async_read_alert,
schema=SERVICE_SCHEMA_READ_ALERT
)
hass.services.async_register(
DOMAIN,
SERVICE_NAME_DELETE_ALERT_ALL,
async_delete_alert_all,
schema=SERVICE_SCHEMA_DELETE_ALERT_ALL
)
hass.services.async_register(
DOMAIN,
SERVICE_NAME_READ_ALERT_ALL,
async_read_alert_all,
schema=SERVICE_SCHEMA_READ_ALERT_ALL
)

hass.data[DOMAIN][CONF_SERVICES_REGISTERED] = entry.entry_id

Expand Down Expand Up @@ -652,13 +818,31 @@ async def _update_alerts(self):
# dependent state updates
if self._indego_client.alerts:
self.entities[ENTITY_ALERT].state = self._indego_client.alerts_count > 0
self.entities[ENTITY_ALERT_COUNT].state = self._indego_client.alerts_count
self.entities[ENTITY_ALERT_ID].state = self._indego_client.alerts[0].alert_id
self.entities[ENTITY_ALERT_ERROR_CODE].state = self._indego_client.alerts[0].error_code
self.entities[ENTITY_ALERT_HEADLINE].state = self._indego_client.alerts[0].headline
self.entities[ENTITY_ALERT_DATE].state = self._indego_client.alerts[0].date
self.entities[ENTITY_ALERT_MESSAGE].state = self._indego_client.alerts[0].message
self.entities[ENTITY_ALERT_READ_STATUS].state = self._indego_client.alerts[0].read_status
self.entities[ENTITY_ALERT_PUSH].state = self._indego_client.alerts[0].push
self.entities[ENTITY_ALERT_DESCRIPTION].state = self._indego_client.alerts[0].alert_description

self.entities[ENTITY_ALERT].add_attribute(
{"alerts_count": self._indego_client.alerts_count, }
)

else:
self.entities[ENTITY_ALERT].state = 0
self.entities[ENTITY_ALERT_COUNT].state = 0
self.entities[ENTITY_ALERT_ID].state = 0
self.entities[ENTITY_ALERT_ERROR_CODE].state = 0
self.entities[ENTITY_ALERT_HEADLINE].state = "Kein Problem"
self.entities[ENTITY_ALERT_DATE].state = "Kein Problem"
self.entities[ENTITY_ALERT_MESSAGE].state = "Kein Problem"
self.entities[ENTITY_ALERT_READ_STATUS].state = False
self.entities[ENTITY_ALERT_PUSH].state = False
self.entities[ENTITY_ALERT_DESCRIPTION].state = "Kein Problem"

j = len(self._indego_client.alerts)
# _LOGGER.info(f"Structuring ALERTS.{j}")
Expand Down
18 changes: 18 additions & 0 deletions custom_components/indego/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@
CONF_SMARTMOWING: Final = "enable"
CONF_POLLING: Final = "polling"

CONF_DELETE_ALERT: Final = "alert_index"
CONF_READ_ALERT: Final = "alert_index"

DEFAULT_NAME: Final = "Indego"
DEFAULT_NAME_COMMANDS: Final = None

SERVICE_NAME_COMMAND: Final = "command"
SERVICE_NAME_SMARTMOW: Final = "smartmowing"
SERVICE_NAME_DELETE_ALERT: Final = "delete_alert"
SERVICE_NAME_READ_ALERT: Final = "read_alert"
SERVICE_NAME_DELETE_ALERT_ALL: Final = "delete_alert_all"
SERVICE_NAME_READ_ALERT_ALL: Final = "read_alert_all"

SENSOR_TYPE: Final = "sensor"
BINARY_SENSOR_TYPE: Final = "binary_sensor"
Expand All @@ -34,6 +41,17 @@
ENTITY_ONLINE: Final = "online"
ENTITY_UPDATE_AVAILABLE: Final = "update_available"
ENTITY_ALERT: Final = "alert"
ENTITY_MOWER_ALERT: Final = "mower_alert"
ENTITY_ALERT_COUNT: Final = "alert_count"
ENTITY_ALERT_ID: Final = "alert_id"
ENTITY_ALERT_ERROR_CODE: Final = "alert_errorcode"
ENTITY_ALERT_HEADLINE: Final = "alert_headline"
ENTITY_ALERT_DATE: Final = "alert_date"
ENTITY_ALERT_MESSAGE: Final = "alert_message"
ENTITY_ALERT_READ_STATUS: Final = "alert_read_status"
ENTITY_ALERT_FLAG: Final = "alert_flag"
ENTITY_ALERT_PUSH: Final = "alert_push"
ENTITY_ALERT_DESCRIPTION: Final = "alert_description"
ENTITY_MOWER_STATE: Final = "mower_state"
ENTITY_MOWER_STATE_DETAIL: Final = "mower_state_detail"
ENTITY_BATTERY: Final = "battery_percentage"
Expand Down
25 changes: 24 additions & 1 deletion custom_components/indego/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,27 @@ smartmowing:
enable:
description: Enable Smart Mow
example: "true"
required: true
required: true
delete_alert:
description: Delete the selected Alert
fields:
alert_index:
description: Delete the selected Alert in this case the latest
example: "0"
read_alert:
description: Read the selected Alert
fields:
alert_index:
description: marked the selected Alert as read
example: "0"
delete_alert_all:
description: Delete the selected Alert
fields:
alert_index:
description: Delete all Alerts
example: "0"
read_alert_all:
description: Read the selected Alert
fields:
alert_index:
description: marked all Alerts as read

0 comments on commit 2a9ae2f

Please sign in to comment.