Skip to content

Commit

Permalink
async_track_state_change > async_track_state_change_event
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Jul 4, 2024
1 parent 4c3398e commit c418dd5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions custom_components/peaqev/peaqservice/hub/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from typing import Callable

# Third party imports
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.event import async_track_state_change
from homeassistant.core import HomeAssistant, callback, Event, EventStateChangedData
from homeassistant.helpers.event import async_track_state_change_event
# Local application imports
from peaqevcore.common.models.observer_types import ObserverTypes
from peaqevcore.common.spotprice.spotpricebase import SpotPriceBase
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self, hass: HomeAssistant, options: HubOptions, domain: str, observ

async def async_setup(self):
trackers = await self.async_setup_tracking()
async_track_state_change(self.state_machine, trackers, self.async_state_changed)
async_track_state_change_event(self.state_machine, trackers, self._async_on_change)

@property
def enabled(self) -> bool:
Expand Down Expand Up @@ -148,7 +148,10 @@ def savings_month(self) -> float:
return 0

@callback
async def async_state_changed(self, entity_id, old_state, new_state):
async def _async_on_change(self, event: Event[EventStateChangedData]) -> None:
entity_id = event.data['entity_id']
old_state = event.data['old_state']
new_state = event.data['new_state']
if entity_id is not None:
try:
if old_state is None or old_state != new_state:
Expand All @@ -158,6 +161,16 @@ async def async_state_changed(self, entity_id, old_state, new_state):
msg = f'Unable to handle data-update: {entity_id} {old_state}|{new_state}. Exception: {e}\n{tb}'
_LOGGER.error(msg)

# async def async_state_changed(self, entity_id, old_state, new_state):
# if entity_id is not None:
# try:
# if old_state is None or old_state != new_state:
# await self.states.async_update_sensor(entity_id, new_state.state)
# except Exception as e:
# tb = traceback.format_exc() # Get the full traceback
# msg = f'Unable to handle data-update: {entity_id} {old_state}|{new_state}. Exception: {e}\n{tb}'
# _LOGGER.error(msg)

async def async_update_adjusted_average(self, val) -> None:
pass

Expand Down

0 comments on commit c418dd5

Please sign in to comment.