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 7 (home-assistant#37870)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
  • Loading branch information
bdraco and balloob authored Jul 15, 2020
1 parent 44fefb3 commit 1d7f341
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
11 changes: 7 additions & 4 deletions homeassistant/components/alert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ def __init__(
self._send_done_message = False
self.entity_id = f"{DOMAIN}.{entity_id}"

event.async_track_state_change(
hass, watched_entity_id, self.watched_entity_change
event.async_track_state_change_event(
hass, [watched_entity_id], self.watched_entity_change
)

@property
Expand All @@ -222,9 +222,12 @@ def state(self):
return STATE_ON
return STATE_IDLE

async def watched_entity_change(self, entity, from_state, to_state):
async def watched_entity_change(self, ev):
"""Determine if the alert should start or stop."""
_LOGGER.debug("Watched entity (%s) has changed", entity)
to_state = ev.data.get("new_state")
if to_state is None:
return
_LOGGER.debug("Watched entity (%s) has changed", ev.data.get("entity_id"))
if to_state.state == self._alert_state and not self._firing:
await self.begin_alerting()
if to_state.state != self._alert_state and self._firing:
Expand Down
11 changes: 8 additions & 3 deletions homeassistant/components/knx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from homeassistant.core import callback
from homeassistant.helpers import discovery
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.script import Script

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -364,17 +364,22 @@ def async_register(self):
self.xknx, name=_name, group_address=self.address, value_type=self.type,
)
self.xknx.devices.add(self.device)
async_track_state_change(self.hass, self.entity_id, self._async_entity_changed)
async_track_state_change_event(
self.hass, [self.entity_id], self._async_entity_changed
)

async def _async_entity_changed(self, entity_id, old_state, new_state):
async def _async_entity_changed(self, event):
"""Handle entity change."""
new_state = event.data.get("new_state")
if new_state is None:
return
if new_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE):
return

if self.expose_attribute is not None:
new_attribute = new_state.attributes.get(self.expose_attribute)
old_state = event.data.get("old_state")

if old_state is not None:
old_attribute = old_state.attributes.get(self.expose_attribute)
if old_attribute == new_attribute:
Expand Down
10 changes: 4 additions & 6 deletions homeassistant/components/zha/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import logging
from typing import Any, Awaitable, Dict, List, Optional

from homeassistant.core import CALLBACK_TYPE, State, callback
from homeassistant.core import CALLBACK_TYPE, Event, callback
from homeassistant.helpers import entity
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
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 .core.const import (
Expand Down Expand Up @@ -245,7 +245,7 @@ async def async_added_to_hass(self) -> None:
signal_override=True,
)

self._async_unsub_state_changed = async_track_state_change(
self._async_unsub_state_changed = async_track_state_change_event(
self.hass, self._entity_ids, self.async_state_changed_listener
)

Expand All @@ -258,9 +258,7 @@ def send_removed_signal():
await self.async_update()

@callback
def async_state_changed_listener(
self, entity_id: str, old_state: State, new_state: State
):
def async_state_changed_listener(self, event: Event):
"""Handle child updates."""
self.async_schedule_update_ha_state(True)

Expand Down
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ persistent=no
extension-pkg-whitelist=ciso8601

[BASIC]
good-names=id,i,j,k,ex,Run,_,fp,T
good-names=id,i,j,k,ex,Run,_,fp,T,ev

[MESSAGES CONTROL]
# Reasons disabled:
Expand Down

0 comments on commit 1d7f341

Please sign in to comment.