Skip to content

Commit

Permalink
Switch a few more async_track_state_change to the faster async_track_…
Browse files Browse the repository at this point in the history
…state_change_event

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
  • Loading branch information
bdraco committed Jul 13, 2020
1 parent 33eaf08 commit 10320bb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/history_stats/sensor.py
Expand Up @@ -20,7 +20,7 @@
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__)
Expand Down Expand Up @@ -128,7 +128,7 @@ def force_refresh(*args):
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)
Expand Down
14 changes: 6 additions & 8 deletions 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

Expand All @@ -13,10 +13,10 @@
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,
Expand Down Expand Up @@ -117,15 +117,13 @@ async def async_added_to_hass(self) -> None:
"""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):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/websocket_api/commands.py
Expand Up @@ -8,7 +8,7 @@
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

Expand Down Expand Up @@ -255,7 +255,7 @@ def state_listener(*_):
)

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:
Expand Down

0 comments on commit 10320bb

Please sign in to comment.