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
This commit is contained in:
J. Nick Koston 2020-07-14 19:24:27 -10:00 committed by GitHub
parent bf72e3c965
commit e65235b207
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 22 deletions

View file

@ -30,7 +30,10 @@ from homeassistant.const import (
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 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity):
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 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity):
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
)

View file

@ -14,7 +14,7 @@ from homeassistant.const import (
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 @@ class MinMaxSensor(Entity):
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 @@ class MinMaxSensor(Entity):
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):

View file

@ -18,7 +18,7 @@ from homeassistant.const import (
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 @@ class MoldIndicator(Entity):
"""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 @@ class MoldIndicator(Entity):
"""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

View file

@ -24,7 +24,7 @@ from homeassistant.exceptions import HomeAssistantError
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 @@ class Plant(Entity):
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 @@ class Plant(Entity):
# 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.

View file

@ -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"