From 20d5d3c162cfee32f30b602bccd0732cb0ac1529 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 14 Jul 2020 19:31:34 -1000 Subject: [PATCH] Switch a few more async_track_state_change to the faster async_track_state_change_event (#37833) async_track_state_change_event is faster than async_track_state_change and since we do not care about the data being returned to the callback this is a simple speedup --- homeassistant/components/history_stats/sensor.py | 4 ++-- homeassistant/components/switch/light.py | 14 ++++++-------- homeassistant/components/websocket_api/commands.py | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/history_stats/sensor.py b/homeassistant/components/history_stats/sensor.py index 48d65145219..2c77261d344 100644 --- a/homeassistant/components/history_stats/sensor.py +++ b/homeassistant/components/history_stats/sensor.py @@ -20,7 +20,7 @@ from homeassistant.core import callback from homeassistant.exceptions import TemplateError 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 import homeassistant.util.dt as dt_util _LOGGER = logging.getLogger(__name__) @@ -128,7 +128,7 @@ class HistoryStatsSensor(Entity): self.async_schedule_update_ha_state(True) force_refresh() - async_track_state_change(self.hass, self._entity_id, force_refresh) + async_track_state_change_event(self.hass, [self._entity_id], force_refresh) # Delay first refresh to keep startup fast hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_refresh) diff --git a/homeassistant/components/switch/light.py b/homeassistant/components/switch/light.py index c23390a3e3e..8b751871014 100644 --- a/homeassistant/components/switch/light.py +++ b/homeassistant/components/switch/light.py @@ -1,6 +1,6 @@ """Light support for switch entities.""" import logging -from typing import Callable, Optional, Sequence, cast +from typing import Any, Callable, Optional, Sequence, cast import voluptuous as vol @@ -13,10 +13,10 @@ from homeassistant.const import ( STATE_ON, STATE_UNAVAILABLE, ) -from homeassistant.core import CALLBACK_TYPE, State, callback +from homeassistant.core import CALLBACK_TYPE, 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 from homeassistant.helpers.typing import ( ConfigType, DiscoveryInfoType, @@ -117,15 +117,13 @@ class LightSwitch(LightEntity): """Register callbacks.""" @callback - def async_state_changed_listener( - entity_id: str, old_state: State, new_state: State - ) -> None: + def async_state_changed_listener(*_: Any) -> None: """Handle child updates.""" self.async_schedule_update_ha_state(True) assert self.hass is not None - self._async_unsub_state_changed = async_track_state_change( - self.hass, self._switch_entity_id, async_state_changed_listener + self._async_unsub_state_changed = async_track_state_change_event( + self.hass, [self._switch_entity_id], async_state_changed_listener ) async def async_will_remove_from_hass(self): diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 3de41bc8918..d7e2fa1ac83 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -8,7 +8,7 @@ from homeassistant.const import EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_A from homeassistant.core import DOMAIN as HASS_DOMAIN, callback from homeassistant.exceptions import HomeAssistantError, ServiceNotFound, Unauthorized from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.service import async_get_all_descriptions from homeassistant.loader import IntegrationNotFound, async_get_integration @@ -255,7 +255,7 @@ def handle_render_template(hass, connection, msg): ) if entity_ids and entity_ids != MATCH_ALL: - connection.subscriptions[msg["id"]] = async_track_state_change( + connection.subscriptions[msg["id"]] = async_track_state_change_event( hass, entity_ids, state_listener ) else: