Cleanup homekit callbacks and jobs (#34975)

pull/34993/head
J. Nick Koston 2020-04-30 17:49:16 -05:00 committed by GitHub
parent 928d9ec117
commit 799d98eaf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 15 deletions

View File

@ -494,7 +494,7 @@ class HomeKit:
self.status = STATUS_STOPPED
_LOGGER.debug("Driver stop")
self.hass.async_add_executor_job(self.driver.stop)
self.hass.add_job(self.driver.stop)
@callback
def _async_configure_linked_battery_sensors(self, ent_reg, device_lookup, state):

View File

@ -165,8 +165,10 @@ class HomeAccessory(Accessory):
Run inside the Home Assistant event loop.
"""
state = self.hass.states.get(self.entity_id)
self.hass.async_add_job(self.update_state_callback, None, None, state)
async_track_state_change(self.hass, self.entity_id, self.update_state_callback)
await self.async_update_state_callback(None, None, state)
async_track_state_change(
self.hass, self.entity_id, self.async_update_state_callback
)
battery_charging_state = None
battery_state = None
@ -179,7 +181,7 @@ class HomeAccessory(Accessory):
ATTR_BATTERY_CHARGING
)
async_track_state_change(
self.hass, self.linked_battery_sensor, self.update_linked_battery
self.hass, self.linked_battery_sensor, self.async_update_linked_battery
)
else:
battery_state = state.attributes.get(ATTR_BATTERY_LEVEL)
@ -191,7 +193,7 @@ class HomeAccessory(Accessory):
async_track_state_change(
self.hass,
self.linked_battery_charging_sensor,
self.update_linked_battery_charging,
self.async_update_linked_battery_charging,
)
elif battery_charging_state is None:
battery_charging_state = state.attributes.get(ATTR_BATTERY_CHARGING)
@ -201,8 +203,9 @@ class HomeAccessory(Accessory):
self.update_battery, battery_state, battery_charging_state
)
@ha_callback
def update_state_callback(self, entity_id=None, old_state=None, new_state=None):
async def async_update_state_callback(
self, entity_id=None, old_state=None, new_state=None
):
"""Handle state change listener callback."""
_LOGGER.debug("New_state: %s", new_state)
if new_state is None:
@ -220,28 +223,28 @@ class HomeAccessory(Accessory):
):
battery_charging_state = new_state.attributes.get(ATTR_BATTERY_CHARGING)
if battery_state is not None or battery_charging_state is not None:
self.hass.async_add_executor_job(
await self.hass.async_add_executor_job(
self.update_battery, battery_state, battery_charging_state
)
self.hass.async_add_executor_job(self.update_state, new_state)
await self.hass.async_add_executor_job(self.update_state, new_state)
@ha_callback
def update_linked_battery(self, entity_id=None, old_state=None, new_state=None):
async def async_update_linked_battery(
self, entity_id=None, old_state=None, new_state=None
):
"""Handle linked battery sensor state change listener callback."""
if self.linked_battery_charging_sensor:
battery_charging_state = None
else:
battery_charging_state = new_state.attributes.get(ATTR_BATTERY_CHARGING)
self.hass.async_add_executor_job(
await self.hass.async_add_executor_job(
self.update_battery, new_state.state, battery_charging_state,
)
@ha_callback
def update_linked_battery_charging(
async def async_update_linked_battery_charging(
self, entity_id=None, old_state=None, new_state=None
):
"""Handle linked battery charging sensor state change listener callback."""
self.hass.async_add_executor_job(
await self.hass.async_add_executor_job(
self.update_battery, None, new_state.state == STATE_ON
)