From e65235b207bc196fc684537ba94bc4e13b400eff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 14 Jul 2020 19:24:27 -1000 Subject: [PATCH] Switch async_track_state_change to the faster async_track_state_change_event part 6 (#37869) Calling async_track_state_change_event directly is faster than async_track_state_change (see #37251) since async_track_state_change is a wrapper around async_track_state_change_event now --- .../manual_mqtt/alarm_control_panel.py | 14 +++++++++---- homeassistant/components/min_max/sensor.py | 11 +++++++--- .../components/mold_indicator/sensor.py | 11 ++++++---- homeassistant/components/plant/__init__.py | 20 ++++++++++++------- tests/components/plant/test_init.py | 5 +---- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/manual_mqtt/alarm_control_panel.py b/homeassistant/components/manual_mqtt/alarm_control_panel.py index 548227173f466..4c760d3dab029 100644 --- a/homeassistant/components/manual_mqtt/alarm_control_panel.py +++ b/homeassistant/components/manual_mqtt/alarm_control_panel.py @@ -30,7 +30,10 @@ STATE_ALARM_TRIGGERED, ) import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.event import async_track_state_change, track_point_in_time +from homeassistant.helpers.event import ( + async_track_state_change_event, + track_point_in_time, +) import homeassistant.util.dt as dt_util _LOGGER = logging.getLogger(__name__) @@ -422,8 +425,8 @@ def device_state_attributes(self): async def async_added_to_hass(self): """Subscribe to MQTT events.""" - async_track_state_change( - self.hass, self.entity_id, self._async_state_changed_listener + async_track_state_change_event( + self.hass, [self.entity_id], self._async_state_changed_listener ) async def message_received(msg): @@ -444,8 +447,11 @@ async def message_received(msg): self.hass, self._command_topic, message_received, self._qos ) - async def _async_state_changed_listener(self, entity_id, old_state, new_state): + async def _async_state_changed_listener(self, event): """Publish state change to MQTT.""" + new_state = event.data.get("new_state") + if new_state is None: + return mqtt.async_publish( self.hass, self._state_topic, new_state.state, self._qos, True ) diff --git a/homeassistant/components/min_max/sensor.py b/homeassistant/components/min_max/sensor.py index a90327874206b..6c99a8db60cf6 100644 --- a/homeassistant/components/min_max/sensor.py +++ b/homeassistant/components/min_max/sensor.py @@ -14,7 +14,7 @@ from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event _LOGGER = logging.getLogger(__name__) @@ -132,8 +132,11 @@ def __init__(self, hass, entity_ids, name, sensor_type, round_digits): self.states = {} @callback - def async_min_max_sensor_state_listener(entity, old_state, new_state): + def async_min_max_sensor_state_listener(event): """Handle the sensor state changes.""" + new_state = event.data.get("new_state") + entity = event.data.get("entity_id") + if new_state.state is None or new_state.state in [ STATE_UNKNOWN, STATE_UNAVAILABLE, @@ -166,7 +169,9 @@ def async_min_max_sensor_state_listener(entity, old_state, new_state): hass.async_add_job(self.async_update_ha_state, True) - async_track_state_change(hass, entity_ids, async_min_max_sensor_state_listener) + async_track_state_change_event( + hass, entity_ids, async_min_max_sensor_state_listener + ) @property def name(self): diff --git a/homeassistant/components/mold_indicator/sensor.py b/homeassistant/components/mold_indicator/sensor.py index 82f1cfaec9bb4..c546a8d33379a 100644 --- a/homeassistant/components/mold_indicator/sensor.py +++ b/homeassistant/components/mold_indicator/sensor.py @@ -18,7 +18,7 @@ from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event _LOGGER = logging.getLogger(__name__) @@ -106,8 +106,11 @@ async def async_added_to_hass(self): """Register callbacks.""" @callback - def mold_indicator_sensors_state_listener(entity, old_state, new_state): + def mold_indicator_sensors_state_listener(event): """Handle for state changes for dependent sensors.""" + new_state = event.data.get("new_state") + old_state = event.data.get("old_state") + entity = event.data.get("entity_id") _LOGGER.debug( "Sensor state change for %s that had old state %s and new state %s", entity, @@ -123,8 +126,8 @@ def mold_indicator_startup(event): """Add listeners and get 1st state.""" _LOGGER.debug("Startup for %s", self.entity_id) - async_track_state_change( - self.hass, self._entities, mold_indicator_sensors_state_listener + async_track_state_change_event( + self.hass, list(self._entities), mold_indicator_sensors_state_listener ) # Read initial state diff --git a/homeassistant/components/plant/__init__.py b/homeassistant/components/plant/__init__.py index 02d6186d79df1..81e27928c6b8e 100644 --- a/homeassistant/components/plant/__init__.py +++ b/homeassistant/components/plant/__init__.py @@ -24,7 +24,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_component import EntityComponent -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event _LOGGER = logging.getLogger(__name__) @@ -183,11 +183,15 @@ def __init__(self, name, config): self._brightness_history = DailyHistory(self._conf_check_days) @callback - def state_changed(self, entity_id, _, new_state): - """Update the sensor status. + def _state_changed_event(self, event): + """Sensor state change event.""" + self.state_changed(event.data.get("entity_id"), event.data.get("new_state")) - This callback is triggered, when the sensor state changes. - """ + @callback + def state_changed(self, entity_id, new_state): + """Update the sensor status.""" + if new_state is None: + return value = new_state.state _LOGGER.debug("Received callback from %s with value %s", entity_id, value) if value == STATE_UNKNOWN: @@ -279,12 +283,14 @@ async def async_added_to_hass(self): # only use the database if it's configured self.hass.async_add_job(self._load_history_from_db) - async_track_state_change(self.hass, list(self._sensormap), self.state_changed) + async_track_state_change_event( + self.hass, list(self._sensormap), self._state_changed_event + ) for entity_id in self._sensormap: state = self.hass.states.get(entity_id) if state is not None: - self.state_changed(entity_id, None, state) + self.state_changed(entity_id, state) async def _load_history_from_db(self): """Load the history of the brightness values from the database. diff --git a/tests/components/plant/test_init.py b/tests/components/plant/test_init.py index f7260a5cf2ead..991eb1d385d67 100644 --- a/tests/components/plant/test_init.py +++ b/tests/components/plant/test_init.py @@ -54,7 +54,6 @@ async def test_valid_data(hass): for reading, value in GOOD_DATA.items(): sensor.state_changed( GOOD_CONFIG["sensors"][reading], - None, State(GOOD_CONFIG["sensors"][reading], value), ) assert sensor.state == "ok" @@ -72,9 +71,7 @@ async def test_low_battery(hass): sensor.hass = hass assert sensor.state_attributes["problem"] == "none" sensor.state_changed( - "sensor.mqtt_plant_battery", - State("sensor.mqtt_plant_battery", 45), - State("sensor.mqtt_plant_battery", 10), + "sensor.mqtt_plant_battery", State("sensor.mqtt_plant_battery", 10), ) assert sensor.state == "problem" assert sensor.state_attributes["problem"] == "battery low"