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:
parent
bf72e3c965
commit
e65235b207
5 changed files with 39 additions and 22 deletions
|
@ -30,7 +30,10 @@ from homeassistant.const import (
|
||||||
STATE_ALARM_TRIGGERED,
|
STATE_ALARM_TRIGGERED,
|
||||||
)
|
)
|
||||||
import homeassistant.helpers.config_validation as cv
|
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
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -422,8 +425,8 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity):
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to MQTT events."""
|
"""Subscribe to MQTT events."""
|
||||||
async_track_state_change(
|
async_track_state_change_event(
|
||||||
self.hass, self.entity_id, self._async_state_changed_listener
|
self.hass, [self.entity_id], self._async_state_changed_listener
|
||||||
)
|
)
|
||||||
|
|
||||||
async def message_received(msg):
|
async def message_received(msg):
|
||||||
|
@ -444,8 +447,11 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity):
|
||||||
self.hass, self._command_topic, message_received, self._qos
|
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."""
|
"""Publish state change to MQTT."""
|
||||||
|
new_state = event.data.get("new_state")
|
||||||
|
if new_state is None:
|
||||||
|
return
|
||||||
mqtt.async_publish(
|
mqtt.async_publish(
|
||||||
self.hass, self._state_topic, new_state.state, self._qos, True
|
self.hass, self._state_topic, new_state.state, self._qos, True
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,7 +14,7 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -132,8 +132,11 @@ class MinMaxSensor(Entity):
|
||||||
self.states = {}
|
self.states = {}
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""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 [
|
if new_state.state is None or new_state.state in [
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
|
@ -166,7 +169,9 @@ class MinMaxSensor(Entity):
|
||||||
|
|
||||||
hass.async_add_job(self.async_update_ha_state, True)
|
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
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
|
|
@ -18,7 +18,7 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -106,8 +106,11 @@ class MoldIndicator(Entity):
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""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(
|
_LOGGER.debug(
|
||||||
"Sensor state change for %s that had old state %s and new state %s",
|
"Sensor state change for %s that had old state %s and new state %s",
|
||||||
entity,
|
entity,
|
||||||
|
@ -123,8 +126,8 @@ class MoldIndicator(Entity):
|
||||||
"""Add listeners and get 1st state."""
|
"""Add listeners and get 1st state."""
|
||||||
_LOGGER.debug("Startup for %s", self.entity_id)
|
_LOGGER.debug("Startup for %s", self.entity_id)
|
||||||
|
|
||||||
async_track_state_change(
|
async_track_state_change_event(
|
||||||
self.hass, self._entities, mold_indicator_sensors_state_listener
|
self.hass, list(self._entities), mold_indicator_sensors_state_listener
|
||||||
)
|
)
|
||||||
|
|
||||||
# Read initial state
|
# Read initial state
|
||||||
|
|
|
@ -24,7 +24,7 @@ from homeassistant.exceptions import HomeAssistantError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -183,11 +183,15 @@ class Plant(Entity):
|
||||||
self._brightness_history = DailyHistory(self._conf_check_days)
|
self._brightness_history = DailyHistory(self._conf_check_days)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def state_changed(self, entity_id, _, new_state):
|
def _state_changed_event(self, event):
|
||||||
"""Update the sensor status.
|
"""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
|
value = new_state.state
|
||||||
_LOGGER.debug("Received callback from %s with value %s", entity_id, value)
|
_LOGGER.debug("Received callback from %s with value %s", entity_id, value)
|
||||||
if value == STATE_UNKNOWN:
|
if value == STATE_UNKNOWN:
|
||||||
|
@ -279,12 +283,14 @@ class Plant(Entity):
|
||||||
# only use the database if it's configured
|
# only use the database if it's configured
|
||||||
self.hass.async_add_job(self._load_history_from_db)
|
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:
|
for entity_id in self._sensormap:
|
||||||
state = self.hass.states.get(entity_id)
|
state = self.hass.states.get(entity_id)
|
||||||
if state is not None:
|
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):
|
async def _load_history_from_db(self):
|
||||||
"""Load the history of the brightness values from the database.
|
"""Load the history of the brightness values from the database.
|
||||||
|
|
|
@ -54,7 +54,6 @@ async def test_valid_data(hass):
|
||||||
for reading, value in GOOD_DATA.items():
|
for reading, value in GOOD_DATA.items():
|
||||||
sensor.state_changed(
|
sensor.state_changed(
|
||||||
GOOD_CONFIG["sensors"][reading],
|
GOOD_CONFIG["sensors"][reading],
|
||||||
None,
|
|
||||||
State(GOOD_CONFIG["sensors"][reading], value),
|
State(GOOD_CONFIG["sensors"][reading], value),
|
||||||
)
|
)
|
||||||
assert sensor.state == "ok"
|
assert sensor.state == "ok"
|
||||||
|
@ -72,9 +71,7 @@ async def test_low_battery(hass):
|
||||||
sensor.hass = hass
|
sensor.hass = hass
|
||||||
assert sensor.state_attributes["problem"] == "none"
|
assert sensor.state_attributes["problem"] == "none"
|
||||||
sensor.state_changed(
|
sensor.state_changed(
|
||||||
"sensor.mqtt_plant_battery",
|
"sensor.mqtt_plant_battery", State("sensor.mqtt_plant_battery", 10),
|
||||||
State("sensor.mqtt_plant_battery", 45),
|
|
||||||
State("sensor.mqtt_plant_battery", 10),
|
|
||||||
)
|
)
|
||||||
assert sensor.state == "problem"
|
assert sensor.state == "problem"
|
||||||
assert sensor.state_attributes["problem"] == "battery low"
|
assert sensor.state_attributes["problem"] == "battery low"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue