Switch async_track_state_change to the faster async_track_state_change_event part 4 (#37863)

* Switch async_track_state_change to the faster async_track_state_change_event part 4

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

* pylint
This commit is contained in:
J. Nick Koston 2020-07-14 19:25:48 -10:00 committed by GitHub
parent aed98a830f
commit b430496b13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 24 deletions

View file

@ -17,7 +17,7 @@ from homeassistant.const import (
)
from homeassistant.core import callback
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.restore_state import RestoreEntity
# mypy: allow-untyped-defs, no-check-untyped-defs
@ -127,8 +127,10 @@ class IntegrationSensor(RestoreEntity):
_LOGGER.warning("Could not restore last state: %s", err)
@callback
def calc_integration(entity, old_state, new_state):
def calc_integration(event):
"""Handle the sensor state changes."""
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
if (
old_state is None
or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE]
@ -174,7 +176,9 @@ class IntegrationSensor(RestoreEntity):
self._state += integral
self.async_write_ha_state()
async_track_state_change(self.hass, self._sensor_source_id, calc_integration)
async_track_state_change_event(
self.hass, [self._sensor_source_id], calc_integration
)
@property
def name(self):