mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Switch async_track_state_change to the faster async_track_state_change_event part 5 (#37866)
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
e65235b207
commit
aed98a830f
@ -44,7 +44,7 @@ from homeassistant.helpers import (
|
|||||||
service,
|
service,
|
||||||
)
|
)
|
||||||
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
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||||
@ -440,14 +440,14 @@ class Person(RestoreEntity):
|
|||||||
if trackers:
|
if trackers:
|
||||||
_LOGGER.debug("Subscribe to device trackers for %s", self.entity_id)
|
_LOGGER.debug("Subscribe to device trackers for %s", self.entity_id)
|
||||||
|
|
||||||
self._unsub_track_device = async_track_state_change(
|
self._unsub_track_device = async_track_state_change_event(
|
||||||
self.hass, trackers, self._async_handle_tracker_update
|
self.hass, trackers, self._async_handle_tracker_update
|
||||||
)
|
)
|
||||||
|
|
||||||
self._update_state()
|
self._update_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_handle_tracker_update(self, entity, old_state, new_state):
|
def _async_handle_tracker_update(self, event):
|
||||||
"""Handle the device tracker state changes."""
|
"""Handle the device tracker state changes."""
|
||||||
self._update_state()
|
self._update_state()
|
||||||
|
|
||||||
|
@ -17,7 +17,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.event import async_track_state_change
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -93,8 +93,12 @@ class ThresholdSensor(BinarySensorEntity):
|
|||||||
self.sensor_value = None
|
self.sensor_value = None
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_threshold_sensor_state_listener(entity, old_state, new_state):
|
def async_threshold_sensor_state_listener(event):
|
||||||
"""Handle sensor state changes."""
|
"""Handle sensor state changes."""
|
||||||
|
new_state = event.data.get("new_state")
|
||||||
|
if new_state is None:
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sensor_value = (
|
self.sensor_value = (
|
||||||
None if new_state.state == STATE_UNKNOWN else float(new_state.state)
|
None if new_state.state == STATE_UNKNOWN else float(new_state.state)
|
||||||
@ -105,7 +109,9 @@ class ThresholdSensor(BinarySensorEntity):
|
|||||||
|
|
||||||
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_id, async_threshold_sensor_state_listener)
|
async_track_state_change_event(
|
||||||
|
hass, [entity_id], async_threshold_sensor_state_listener
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -25,7 +25,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 generate_entity_id
|
from homeassistant.helpers.entity import generate_entity_id
|
||||||
from homeassistant.helpers.event import async_track_state_change
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
from homeassistant.util import utcnow
|
from homeassistant.util import utcnow
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -162,8 +162,11 @@ class SensorTrend(BinarySensorEntity):
|
|||||||
"""Complete device setup after being added to hass."""
|
"""Complete device setup after being added to hass."""
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def trend_sensor_state_listener(entity, old_state, new_state):
|
def trend_sensor_state_listener(event):
|
||||||
"""Handle state changes on the observed device."""
|
"""Handle state changes on the observed device."""
|
||||||
|
new_state = event.data.get("new_state")
|
||||||
|
if new_state is None:
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
if self._attribute:
|
if self._attribute:
|
||||||
state = new_state.attributes.get(self._attribute)
|
state = new_state.attributes.get(self._attribute)
|
||||||
@ -176,8 +179,8 @@ class SensorTrend(BinarySensorEntity):
|
|||||||
except (ValueError, TypeError) as ex:
|
except (ValueError, TypeError) as ex:
|
||||||
_LOGGER.error(ex)
|
_LOGGER.error(ex)
|
||||||
|
|
||||||
async_track_state_change(
|
async_track_state_change_event(
|
||||||
self.hass, self._entity_id, trend_sensor_state_listener
|
self.hass, [self._entity_id], trend_sensor_state_listener
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
|
@ -16,7 +16,7 @@ from homeassistant.core import callback
|
|||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.event import (
|
from homeassistant.helpers.event import (
|
||||||
async_track_state_change,
|
async_track_state_change_event,
|
||||||
async_track_time_change,
|
async_track_time_change,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
@ -131,8 +131,10 @@ class UtilityMeterSensor(RestoreEntity):
|
|||||||
self._tariff_entity = tariff_entity
|
self._tariff_entity = tariff_entity
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_reading(self, entity, old_state, new_state):
|
def async_reading(self, event):
|
||||||
"""Handle the sensor state changes."""
|
"""Handle the sensor state changes."""
|
||||||
|
old_state = event.data.get("old_state")
|
||||||
|
new_state = event.data.get("new_state")
|
||||||
if (
|
if (
|
||||||
old_state is None
|
old_state is None
|
||||||
or new_state is None
|
or new_state is None
|
||||||
@ -166,11 +168,14 @@ class UtilityMeterSensor(RestoreEntity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_tariff_change(self, entity, old_state, new_state):
|
def async_tariff_change(self, event):
|
||||||
"""Handle tariff changes."""
|
"""Handle tariff changes."""
|
||||||
|
new_state = event.data.get("new_state")
|
||||||
|
if new_state is None:
|
||||||
|
return
|
||||||
if self._tariff == new_state.state:
|
if self._tariff == new_state.state:
|
||||||
self._collecting = async_track_state_change(
|
self._collecting = async_track_state_change_event(
|
||||||
self.hass, self._sensor_source_id, self.async_reading
|
self.hass, [self._sensor_source_id], self.async_reading
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
if self._collecting:
|
if self._collecting:
|
||||||
@ -263,8 +268,8 @@ class UtilityMeterSensor(RestoreEntity):
|
|||||||
"""Wait for source to be ready, then start meter."""
|
"""Wait for source to be ready, then start meter."""
|
||||||
if self._tariff_entity is not None:
|
if self._tariff_entity is not None:
|
||||||
_LOGGER.debug("Track %s", self._tariff_entity)
|
_LOGGER.debug("Track %s", self._tariff_entity)
|
||||||
async_track_state_change(
|
async_track_state_change_event(
|
||||||
self.hass, self._tariff_entity, self.async_tariff_change
|
self.hass, [self._tariff_entity], self.async_tariff_change
|
||||||
)
|
)
|
||||||
|
|
||||||
tariff_entity_state = self.hass.states.get(self._tariff_entity)
|
tariff_entity_state = self.hass.states.get(self._tariff_entity)
|
||||||
@ -272,8 +277,8 @@ class UtilityMeterSensor(RestoreEntity):
|
|||||||
return
|
return
|
||||||
|
|
||||||
_LOGGER.debug("tracking source: %s", self._sensor_source_id)
|
_LOGGER.debug("tracking source: %s", self._sensor_source_id)
|
||||||
self._collecting = async_track_state_change(
|
self._collecting = async_track_state_change_event(
|
||||||
self.hass, self._sensor_source_id, self.async_reading
|
self.hass, [self._sensor_source_id], self.async_reading
|
||||||
)
|
)
|
||||||
|
|
||||||
self.hass.bus.async_listen_once(
|
self.hass.bus.async_listen_once(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user