Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Deprecate deebot.refresh (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Jan 2, 2024
1 parent 6e9dfc0 commit 562a9df
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 61 deletions.
4 changes: 1 addition & 3 deletions custom_components/deebot/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,4 @@ async def on_event(event: EventT) -> None:
self._attr_icon = self.entity_description.icon_fn(self._attr_is_on)
self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_event)
)
self._subscribe(self._capability.event, on_event)
26 changes: 21 additions & 5 deletions custom_components/deebot/entity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Deebot entity module."""
from collections.abc import Callable
from collections.abc import Callable, Coroutine
from dataclasses import dataclass
from typing import Any, Generic, TypeVar

Expand Down Expand Up @@ -33,8 +33,9 @@ class DeebotEntity(Entity, Generic[CapabilityT, _EntityDescriptionT]): # type:
entity_description: _EntityDescriptionT

_attr_should_poll = False
_always_available: bool = False
_attr_has_entity_name = True
_always_available: bool = False
_subscribed_events: set[type[Event]] = set()

def __init__(
self,
Expand Down Expand Up @@ -92,6 +93,21 @@ async def on_available(event: AvailabilityEvent) -> None:
self._attr_available = event.available
self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(AvailabilityEvent, on_available)
)
self._subscribe(AvailabilityEvent, on_available)

def _subscribe(
self,
event_type: type[EventT],
callback: Callable[[EventT], Coroutine[Any, Any, None]],
) -> None:
"""Subscribe to events."""
self._subscribed_events.add(event_type)
self.async_on_remove(self._device.events.subscribe(event_type, callback))

async def async_update(self) -> None:
"""Update the entity.
Only used by the generic entity update service.
"""
for event_type in self._subscribed_events:
self._device.events.request_refresh(event_type)
19 changes: 11 additions & 8 deletions custom_components/deebot/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,26 @@ async def async_added_to_hass(self) -> None:
"""Set up the event listeners now that hass is ready."""
await super().async_added_to_hass()

self._device.map.enable()

async def on_info(event: CachedMapInfoEvent) -> None:
self._attr_extra_state_attributes["map_name"] = event.name

async def on_changed(event: MapChangedEvent) -> None:
self._attr_image_last_updated = event.when
self.async_write_ha_state()

subscriptions = [
self._device.events.subscribe(self._capability.chached_info.event, on_info),
self._device.events.subscribe(self._capability.changed.event, on_changed),
]
self._subscribe(self._capability.chached_info.event, on_info)
self._subscribe(self._capability.changed.event, on_changed)

def on_remove() -> None:
for unsubscribe in subscriptions:
unsubscribe()
self._device.map.disable()

self.async_on_remove(on_remove)
self._device.map.enable()

async def async_update(self) -> None:
"""Update the entity.
Only used by the generic entity update service.
"""
await super().async_update()
self._device.map.refresh()
4 changes: 1 addition & 3 deletions custom_components/deebot/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ async def on_event(event: EventT) -> None:
self._attr_icon = icon
self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_event)
)
self._subscribe(self._capability.event, on_event)

async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
Expand Down
6 changes: 2 additions & 4 deletions custom_components/deebot/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ async def async_added_to_hass(self) -> None:
"""Set up the event listeners now that hass is ready."""
await super().async_added_to_hass()

async def on_water_info(event: EventT) -> None:
async def on_event(event: EventT) -> None:
self._attr_current_option = self.entity_description.current_option_fn(event)
self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_water_info)
)
self._subscribe(self._capability.event, on_event)

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
Expand Down
16 changes: 4 additions & 12 deletions custom_components/deebot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ async def on_event(event: Event) -> None:
self._attr_extra_state_attributes = attr_fn(event)
self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_event)
)
self._subscribe(self._capability.event, on_event)


T = TypeVar("T", bound=Event)
Expand All @@ -302,9 +300,7 @@ async def on_event(event: LifeSpanEvent) -> None:
}
self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_event)
)
self._subscribe(self._capability.event, on_event)


class LastErrorSensor(
Expand Down Expand Up @@ -333,9 +329,7 @@ async def on_event(event: ErrorEvent) -> None:

self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_event)
)
self._subscribe(self._capability.event, on_event)


class LastCleaningSensor(
Expand Down Expand Up @@ -370,6 +364,4 @@ async def on_event(event: CleanLogEvent) -> None:

self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_event)
)
self._subscribe(self._capability.event, on_event)
6 changes: 2 additions & 4 deletions custom_components/deebot/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,11 @@ async def async_added_to_hass(self) -> None:
"""Set up the event listeners now that hass is ready."""
await super().async_added_to_hass()

async def on_enable(event: EnableEvent) -> None:
async def on_event(event: EnableEvent) -> None:
self._attr_is_on = event.enable
self.async_write_ha_state()

self.async_on_remove(
self._device.events.subscribe(self._capability.event, on_enable)
)
self._subscribe(self._capability.event, on_event)

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
Expand Down
13 changes: 13 additions & 0 deletions custom_components/deebot/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@
}
}
},
"issues": {
"deprecated_service_refresh": {
"fix_flow": {
"step": {
"confirm": {
"description": "The service `deebot.refresh` is deprecated and will be removed.\nTo refresh an entity please use `homeassistant.update_entity` on the respective entity instead.\n\nPlease remove this service from your automations and scripts and select **submit** to close this issue.",
"title": "Deebot refresh service is being removed"
}
}
},
"title": "Deebot refresh service is being removed"
}
},
"options": {
"abort": {
"cannot_connect": "Can't connect to the ecovacs API. Please check the logs",
Expand Down
41 changes: 19 additions & 22 deletions custom_components/deebot/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from homeassistant.helpers import entity_platform
from homeassistant.helpers.config_validation import make_entity_service_schema
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.util import slugify

from .const import (
Expand Down Expand Up @@ -158,31 +159,15 @@ async def on_status(event: StateEvent) -> None:
self._attr_state = _STATE_TO_VACUUM_STATE[event.state]
self.async_write_ha_state()

subscriptions = [
self._device.events.subscribe(self._capability.battery.event, on_battery),
self._device.events.subscribe(
self._capability.fan_speed.event, on_fan_speed
),
self._device.events.subscribe(
self._capability.stats.report.event, on_report_stats
),
self._device.events.subscribe(self._capability.state.event, on_status),
]
self._subscribe(self._capability.battery.event, on_battery)
self._subscribe(self._capability.fan_speed.event, on_fan_speed)
self._subscribe(self._capability.stats.report.event, on_report_stats)
self._subscribe(self._capability.state.event, on_status)

if custom := self._capability.custom:
subscriptions.append(
self._device.events.subscribe(custom.event, on_custom_command)
)
self._subscribe(custom.event, on_custom_command)
if map_caps := self._capability.map:
subscriptions.append(
self._device.events.subscribe(map_caps.rooms.event, on_rooms)
)

def unsubscribe() -> None:
for sub in subscriptions:
sub()

self.async_on_remove(unsubscribe)
self._subscribe(map_caps.rooms.event, on_rooms)

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
Expand Down Expand Up @@ -272,6 +257,18 @@ async def async_send_command(

async def service_refresh(self, category: str) -> None:
"""Service to manually refresh."""
_LOGGER.warning(
'Service "deebot.refresh" is deprecated. To refresh an entity please use "homeassistant.update_entity" on the respective entity instead'
)
async_create_issue(
self.hass,
DOMAIN,
"deprecated_service_refresh",
is_fixable=True,
severity=IssueSeverity.WARNING,
translation_key="deprecated_service_refresh",
)

_LOGGER.debug("Manually refresh %s", category)
event = REFRESH_STR_TO_EVENT_DTO.get(category, None)
if event:
Expand Down
39 changes: 39 additions & 0 deletions translations.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,45 @@
},
"type": "object"
},
"issues": {
"additionalProperties": false,
"properties": {
"deprecated_service_refresh": {
"additionalProperties": false,
"properties": {
"fix_flow": {
"additionalProperties": false,
"properties": {
"step": {
"additionalProperties": false,
"properties": {
"confirm": {
"additionalProperties": false,
"properties": {
"description": {
"type": "string"
},
"title": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
},
"title": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"options": {
"additionalProperties": false,
"properties": {
Expand Down

0 comments on commit 562a9df

Please sign in to comment.