Skip to content

Commit

Permalink
Merge pull request #7 from litinoveweedle/fix_async_track_state_chang…
Browse files Browse the repository at this point in the history
…e_deprecation

fixed async_track_state_change deprecation for fan entity
  • Loading branch information
litinoveweedle authored May 20, 2024
2 parents c5762b3 + 7ab711a commit 1d969df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ async def _async_power_sensor_changed(
"""Handle power sensor changes."""
old_state = event.data["old_state"]
new_state = event.data["new_state"]
if new_state is None or new_state == old_state:
if new_state is None:
return

if old_state is not None and new_state.state == old_state.state:
Expand Down
20 changes: 12 additions & 8 deletions custom_components/smartir/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
ATTR_OSCILLATING,
)
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import callback
from homeassistant.helpers.event import async_track_state_change
from homeassistant.core import Event, EventStateChangedData
from homeassistant.helpers.event import async_track_state_change_event
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util.percentage import (
Expand Down Expand Up @@ -146,10 +146,10 @@ async def async_added_to_hass(self):
if "last_on_speed" in last_state.attributes:
self._last_on_speed = last_state.attributes["last_on_speed"]

if self._power_sensor:
async_track_state_change(
self.hass, self._power_sensor, self._async_power_sensor_changed
)
if self._power_sensor:
async_track_state_change_event(
self.hass, self._power_sensor, self._async_power_sensor_changed
)

@property
def unique_id(self):
Expand Down Expand Up @@ -276,12 +276,16 @@ async def send_command(self):
except Exception as e:
_LOGGER.exception(e)

async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
async def _async_power_sensor_changed(
self, event: Event[EventStateChangedData]
) -> None:
"""Handle power sensor changes."""
old_state = event.data["old_state"]
new_state = event.data["new_state"]
if new_state is None:
return

if new_state.state == old_state.state:
if old_state is not None and new_state.state == old_state.state:
return

if new_state.state == STATE_ON and self._speed == SPEED_OFF:
Expand Down

0 comments on commit 1d969df

Please sign in to comment.