IceBotYT cd3059aa14
Nice G.O. code quality improvements (#124319)
* Bring Nice G.O. up to platinum

* Switch to listen in coordinator

* Tests

* Remove parallel updates from coordinator

* Unsub from events on config entry unload

* Detect WS disconnection

* Tests

* Fix tests

* Set unsub to None after unsubbing

* Wait 5 seconds before setting update error to prevent excessive errors

* Tweaks

* More tweaks

* Tweaks part 2

* Potential test for hass stopping

* Improve reconnect handling and test on Homeassistant stop event

* Move event handler to entry init

* Patch const instead of asyncio.sleep

---------

Co-authored-by: jbouwh <jan@jbsoft.nl>
2024-09-06 18:22:59 +02:00

56 lines
1.6 KiB
Python

"""Nice G.O. event platform."""
import logging
from typing import Any
from homeassistant.components.event import EventEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import NiceGOConfigEntry
from .entity import NiceGOEntity
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: NiceGOConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Nice G.O. event."""
coordinator = config_entry.runtime_data
async_add_entities(
NiceGOEventEntity(coordinator, device_id, device_data.name)
for device_id, device_data in coordinator.data.items()
)
EVENT_BARRIER_OBSTRUCTED = "barrier_obstructed"
class NiceGOEventEntity(NiceGOEntity, EventEntity):
"""Event for Nice G.O. devices."""
_attr_translation_key = "barrier_obstructed"
_attr_event_types = [EVENT_BARRIER_OBSTRUCTED]
async def async_added_to_hass(self) -> None:
"""Listen for events."""
await super().async_added_to_hass()
self.async_on_remove(
self.coordinator.api.listen(
"on_barrier_obstructed", self.on_barrier_obstructed
)
)
async def on_barrier_obstructed(self, data: dict[str, Any]) -> None:
"""Handle barrier obstructed event."""
_LOGGER.debug("Barrier obstructed event: %s", data)
if data["deviceId"] == self.data.id:
_LOGGER.debug("Barrier obstructed event for %s, triggering", self.data.name)
self._trigger_event(EVENT_BARRIER_OBSTRUCTED)
self.async_write_ha_state()