Skip to content

Commit

Permalink
Merge pull request #123 from basbruss/async_event
Browse files Browse the repository at this point in the history
Update async event listeners
  • Loading branch information
basbruss authored Apr 28, 2024
2 parents 335cd1b + 5c11acf commit a5eb5ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions custom_components/adaptive_cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.event import (
async_track_state_change,
async_track_state_change_event,
)

from .const import (
Expand Down Expand Up @@ -48,15 +48,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_entities.append(entity)

entry.async_on_unload(
async_track_state_change(
async_track_state_change_event(
hass,
_entities,
coordinator.async_check_entity_state_change,
)
)

entry.async_on_unload(
async_track_state_change(
async_track_state_change_event(
hass,
_cover_entities,
coordinator.async_check_cover_state_change,
Expand Down
13 changes: 7 additions & 6 deletions custom_components/adaptive_cover/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
SERVICE_SET_COVER_POSITION,
SERVICE_SET_COVER_TILT_POSITION,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, State
from homeassistant.helpers.template import state_attr
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

Expand Down Expand Up @@ -120,20 +120,21 @@ async def async_config_entry_first_refresh(self) -> None:
await super().async_config_entry_first_refresh()
_LOGGER.debug("Config entry first refresh")

async def async_check_entity_state_change(
self, entity: str, old_state: State | None, new_state: State | None
) -> None:
async def async_check_entity_state_change(self) -> None:
"""Fetch and process state change event."""
_LOGGER.debug("Entity state change")
self.state_change = True
await self.async_refresh()

async def async_check_cover_state_change(
self, entity: str, old_state: State | None, new_state: State | None
self, event: Event[EventStateChangedData]
) -> None:
"""Fetch and process state change event."""
_LOGGER.debug("Cover state change")
self.state_change_data = StateChangedData(entity, old_state, new_state)
data = event.data
self.state_change_data = StateChangedData(
data["entity_id"], data["old_state"], data["new_state"]
)
self.cover_state_change = True
self.process_entity_state_change()
await self.async_refresh()
Expand Down

0 comments on commit a5eb5ef

Please sign in to comment.