Skip to content

Commit

Permalink
Switch async_track_state_change to the faster async_track_state_chang…
Browse files Browse the repository at this point in the history
…e_event part 6 (home-assistant#37869)

Calling async_track_state_change_event directly is faster than async_track_state_change (see home-assistant#37251) since async_track_state_change is a wrapper around async_track_state_change_event now
  • Loading branch information
bdraco authored Jul 15, 2020
1 parent bf72e3c commit e65235b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
14 changes: 10 additions & 4 deletions homeassistant/components/manual_mqtt/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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):
Expand All @@ -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
)
11 changes: 8 additions & 3 deletions homeassistant/components/min_max/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 7 additions & 4 deletions homeassistant/components/mold_indicator/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
20 changes: 13 additions & 7 deletions homeassistant/components/plant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions tests/components/plant/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit e65235b

Please sign in to comment.