diff --git a/homeassistant/components/derivative/sensor.py b/homeassistant/components/derivative/sensor.py index 65bbd9affee..c1369dd0f5b 100644 --- a/homeassistant/components/derivative/sensor.py +++ b/homeassistant/components/derivative/sensor.py @@ -18,7 +18,7 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.restore_state import RestoreEntity # mypy: allow-untyped-defs, no-check-untyped-defs @@ -130,8 +130,10 @@ class DerivativeSensor(RestoreEntity): _LOGGER.warning("Could not restore last state: %s", err) @callback - def calc_derivative(entity, old_state, new_state): + def calc_derivative(event): """Handle the sensor state changes.""" + old_state = event.data.get("old_state") + new_state = event.data.get("new_state") if ( old_state is None or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE] @@ -184,7 +186,9 @@ class DerivativeSensor(RestoreEntity): self._state = derivative self.async_write_ha_state() - async_track_state_change(self.hass, self._sensor_source_id, calc_derivative) + async_track_state_change_event( + self.hass, [self._sensor_source_id], calc_derivative + ) @property def name(self): diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index 407923dc161..be0ec93f225 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -37,7 +37,7 @@ from homeassistant.core import DOMAIN as HA_DOMAIN, callback from homeassistant.helpers import condition import homeassistant.helpers.config_validation as cv from homeassistant.helpers.event import ( - async_track_state_change, + async_track_state_change_event, async_track_time_interval, ) from homeassistant.helpers.restore_state import RestoreEntity @@ -182,11 +182,11 @@ class GenericThermostat(ClimateEntity, RestoreEntity): await super().async_added_to_hass() # Add listener - async_track_state_change( - self.hass, self.sensor_entity_id, self._async_sensor_changed + async_track_state_change_event( + self.hass, [self.sensor_entity_id], self._async_sensor_changed ) - async_track_state_change( - self.hass, self.heater_entity_id, self._async_switch_changed + async_track_state_change_event( + self.hass, [self.heater_entity_id], self._async_switch_changed ) if self._keep_alive: @@ -354,8 +354,9 @@ class GenericThermostat(ClimateEntity, RestoreEntity): # Get default temp from super class return super().max_temp - async def _async_sensor_changed(self, entity_id, old_state, new_state): + async def _async_sensor_changed(self, event): """Handle temperature changes.""" + new_state = event.data.get("new_state") if new_state is None or new_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN): return @@ -364,8 +365,9 @@ class GenericThermostat(ClimateEntity, RestoreEntity): self.async_write_ha_state() @callback - def _async_switch_changed(self, entity_id, old_state, new_state): + def _async_switch_changed(self, event): """Handle heater switch state changes.""" + new_state = event.data.get("new_state") if new_state is None: return self.async_write_ha_state() diff --git a/homeassistant/components/integration/sensor.py b/homeassistant/components/integration/sensor.py index 3365e5b10c8..6169e084a02 100644 --- a/homeassistant/components/integration/sensor.py +++ b/homeassistant/components/integration/sensor.py @@ -17,7 +17,7 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.restore_state import RestoreEntity # mypy: allow-untyped-defs, no-check-untyped-defs @@ -127,8 +127,10 @@ class IntegrationSensor(RestoreEntity): _LOGGER.warning("Could not restore last state: %s", err) @callback - def calc_integration(entity, old_state, new_state): + def calc_integration(event): """Handle the sensor state changes.""" + old_state = event.data.get("old_state") + new_state = event.data.get("new_state") if ( old_state is None or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE] @@ -174,7 +176,9 @@ class IntegrationSensor(RestoreEntity): self._state += integral self.async_write_ha_state() - async_track_state_change(self.hass, self._sensor_source_id, calc_integration) + async_track_state_change_event( + self.hass, [self._sensor_source_id], calc_integration + ) @property def name(self): diff --git a/homeassistant/components/statistics/sensor.py b/homeassistant/components/statistics/sensor.py index d5fcfbf31ae..945e5ff89d1 100644 --- a/homeassistant/components/statistics/sensor.py +++ b/homeassistant/components/statistics/sensor.py @@ -17,8 +17,12 @@ from homeassistant.const import ( STATE_UNKNOWN, ) from homeassistant.core import callback -from homeassistant.helpers import config_validation as cv, event +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity import Entity +from homeassistant.helpers.event import ( + async_track_point_in_utc_time, + async_track_state_change_event, +) from homeassistant.util import dt as dt_util _LOGGER = logging.getLogger(__name__) @@ -101,8 +105,12 @@ class StatisticsSensor(Entity): """Register callbacks.""" @callback - def async_stats_sensor_state_listener(entity, old_state, new_state): + def async_stats_sensor_state_listener(event): """Handle the sensor state changes.""" + new_state = event.data.get("new_state") + if new_state is None: + return + self._unit_of_measurement = new_state.attributes.get( ATTR_UNIT_OF_MEASUREMENT ) @@ -116,8 +124,8 @@ class StatisticsSensor(Entity): """Add listener and get recorded state.""" _LOGGER.debug("Startup for %s", self.entity_id) - event.async_track_state_change( - self.hass, self._entity_id, async_stats_sensor_state_listener + async_track_state_change_event( + self.hass, [self._entity_id], async_stats_sensor_state_listener ) if "recorder" in self.hass.config.components: @@ -292,7 +300,7 @@ class StatisticsSensor(Entity): self.async_schedule_update_ha_state(True) self._update_listener = None - self._update_listener = event.async_track_point_in_utc_time( + self._update_listener = async_track_point_in_utc_time( self.hass, _scheduled_update, next_to_purge_timestamp ) diff --git a/tests/components/statistics/test_sensor.py b/tests/components/statistics/test_sensor.py index f013d374106..6d7f0963fa2 100644 --- a/tests/components/statistics/test_sensor.py +++ b/tests/components/statistics/test_sensor.py @@ -184,7 +184,10 @@ class TestStatisticsSensor(unittest.TestCase): def test_max_age(self): """Test value deprecation.""" - mock_data = {"return_time": datetime(2017, 8, 2, 12, 23, tzinfo=dt_util.UTC)} + now = dt_util.utcnow() + mock_data = { + "return_time": datetime(now.year + 1, 8, 2, 12, 23, tzinfo=dt_util.UTC) + } def mock_now(): return mock_data["return_time"] @@ -226,7 +229,10 @@ class TestStatisticsSensor(unittest.TestCase): def test_max_age_without_sensor_change(self): """Test value deprecation.""" - mock_data = {"return_time": datetime(2017, 8, 2, 12, 23, tzinfo=dt_util.UTC)} + now = dt_util.utcnow() + mock_data = { + "return_time": datetime(now.year + 1, 8, 2, 12, 23, tzinfo=dt_util.UTC) + } def mock_now(): return mock_data["return_time"] @@ -279,8 +285,9 @@ class TestStatisticsSensor(unittest.TestCase): def test_change_rate(self): """Test min_age/max_age and change_rate.""" + now = dt_util.utcnow() mock_data = { - "return_time": datetime(2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC) + "return_time": datetime(now.year + 1, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC) } def mock_now(): @@ -318,10 +325,10 @@ class TestStatisticsSensor(unittest.TestCase): state = self.hass.states.get("sensor.test") assert datetime( - 2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC + now.year + 1, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC ) == state.attributes.get("min_age") assert datetime( - 2017, 8, 2, 12, 23 + self.count - 1, 42, tzinfo=dt_util.UTC + now.year + 1, 8, 2, 12, 23 + self.count - 1, 42, tzinfo=dt_util.UTC ) == state.attributes.get("max_age") assert self.change_rate == state.attributes.get("change_rate") @@ -364,8 +371,9 @@ class TestStatisticsSensor(unittest.TestCase): def test_initialize_from_database_with_maxage(self): """Test initializing the statistics from the database.""" + now = dt_util.utcnow() mock_data = { - "return_time": datetime(2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC) + "return_time": datetime(now.year + 1, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC) } def mock_now():