Switch async_track_state_change to the faster async_track_state_change_event (#37834)

async_track_state_change_event is faster than async_track_state_change
This commit is contained in:
J. Nick Koston 2020-07-14 19:30:47 -10:00 committed by GitHub
parent b430496b13
commit b12566e265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 20 deletions

View File

@ -32,7 +32,7 @@ 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, async_generate_entity_id from homeassistant.helpers.entity import Entity, async_generate_entity_id
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.typing import HomeAssistantType from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
@ -499,7 +499,7 @@ class Group(Entity):
This method must be run in the event loop. This method must be run in the event loop.
""" """
if self._async_unsub_state_changed is None: if self._async_unsub_state_changed is None:
self._async_unsub_state_changed = async_track_state_change( self._async_unsub_state_changed = async_track_state_change_event(
self.hass, self.tracking, self._async_state_changed_listener self.hass, self.tracking, self._async_state_changed_listener
) )
@ -528,7 +528,7 @@ class Group(Entity):
self._async_unsub_state_changed() self._async_unsub_state_changed()
self._async_unsub_state_changed = None self._async_unsub_state_changed = None
async def _async_state_changed_listener(self, entity_id, old_state, new_state): async def _async_state_changed_listener(self, event):
"""Respond to a member state changing. """Respond to a member state changing.
This method must be run in the event loop. This method must be run in the event loop.
@ -537,7 +537,7 @@ class Group(Entity):
if self._async_unsub_state_changed is None: if self._async_unsub_state_changed is None:
return return
self._async_update_group_state(new_state) self._async_update_group_state(event.data.get("new_state"))
self.async_write_ha_state() self.async_write_ha_state()
@property @property

View File

@ -41,7 +41,7 @@ from homeassistant.const import (
) )
from homeassistant.core import State, callback from homeassistant.core import State, 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
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs # mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
# mypy: no-check-untyped-defs # mypy: no-check-untyped-defs
@ -94,13 +94,15 @@ class CoverGroup(CoverEntity):
KEY_POSITION: set(), KEY_POSITION: set(),
} }
@callback
def _update_supported_features_event(self, event):
self.update_supported_features(
event.data.get("entity_id"), event.data.get("new_state")
)
@callback @callback
def update_supported_features( def update_supported_features(
self, self, entity_id: str, new_state: Optional[State], update_state: bool = True,
entity_id: str,
old_state: Optional[State],
new_state: Optional[State],
update_state: bool = True,
) -> None: ) -> None:
"""Update dictionaries with supported features.""" """Update dictionaries with supported features."""
if not new_state: if not new_state:
@ -147,11 +149,9 @@ class CoverGroup(CoverEntity):
"""Register listeners.""" """Register listeners."""
for entity_id in self._entities: for entity_id in self._entities:
new_state = self.hass.states.get(entity_id) new_state = self.hass.states.get(entity_id)
self.update_supported_features( self.update_supported_features(entity_id, new_state, update_state=False)
entity_id, None, new_state, update_state=False async_track_state_change_event(
) self.hass, self._entities, self._update_supported_features_event
async_track_state_change(
self.hass, self._entities, self.update_supported_features
) )
await self.async_update() await self.async_update()

View File

@ -38,7 +38,7 @@ from homeassistant.const import (
) )
from homeassistant.core import CALLBACK_TYPE, State, callback from homeassistant.core import CALLBACK_TYPE, State, 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
from homeassistant.helpers.typing import ConfigType, HomeAssistantType from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.util import color as color_util from homeassistant.util import color as color_util
@ -100,14 +100,12 @@ class LightGroup(light.LightEntity):
"""Register callbacks.""" """Register callbacks."""
@callback @callback
def async_state_changed_listener( def async_state_changed_listener(*_):
entity_id: str, old_state: State, new_state: State
):
"""Handle child updates.""" """Handle child updates."""
self.async_schedule_update_ha_state(True) self.async_schedule_update_ha_state(True)
assert self.hass is not None assert self.hass is not None
self._async_unsub_state_changed = async_track_state_change( self._async_unsub_state_changed = async_track_state_change_event(
self.hass, self._entity_ids, async_state_changed_listener self.hass, self._entity_ids, async_state_changed_listener
) )
await self.async_update() await self.async_update()