Cleanup homekit callbacks and jobs (#34975)
parent
928d9ec117
commit
799d98eaf0
|
@ -494,7 +494,7 @@ class HomeKit:
|
||||||
self.status = STATUS_STOPPED
|
self.status = STATUS_STOPPED
|
||||||
|
|
||||||
_LOGGER.debug("Driver stop")
|
_LOGGER.debug("Driver stop")
|
||||||
self.hass.async_add_executor_job(self.driver.stop)
|
self.hass.add_job(self.driver.stop)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_configure_linked_battery_sensors(self, ent_reg, device_lookup, state):
|
def _async_configure_linked_battery_sensors(self, ent_reg, device_lookup, state):
|
||||||
|
|
|
@ -165,8 +165,10 @@ class HomeAccessory(Accessory):
|
||||||
Run inside the Home Assistant event loop.
|
Run inside the Home Assistant event loop.
|
||||||
"""
|
"""
|
||||||
state = self.hass.states.get(self.entity_id)
|
state = self.hass.states.get(self.entity_id)
|
||||||
self.hass.async_add_job(self.update_state_callback, None, None, state)
|
await self.async_update_state_callback(None, None, state)
|
||||||
async_track_state_change(self.hass, self.entity_id, self.update_state_callback)
|
async_track_state_change(
|
||||||
|
self.hass, self.entity_id, self.async_update_state_callback
|
||||||
|
)
|
||||||
|
|
||||||
battery_charging_state = None
|
battery_charging_state = None
|
||||||
battery_state = None
|
battery_state = None
|
||||||
|
@ -179,7 +181,7 @@ class HomeAccessory(Accessory):
|
||||||
ATTR_BATTERY_CHARGING
|
ATTR_BATTERY_CHARGING
|
||||||
)
|
)
|
||||||
async_track_state_change(
|
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:
|
else:
|
||||||
battery_state = state.attributes.get(ATTR_BATTERY_LEVEL)
|
battery_state = state.attributes.get(ATTR_BATTERY_LEVEL)
|
||||||
|
@ -191,7 +193,7 @@ class HomeAccessory(Accessory):
|
||||||
async_track_state_change(
|
async_track_state_change(
|
||||||
self.hass,
|
self.hass,
|
||||||
self.linked_battery_charging_sensor,
|
self.linked_battery_charging_sensor,
|
||||||
self.update_linked_battery_charging,
|
self.async_update_linked_battery_charging,
|
||||||
)
|
)
|
||||||
elif battery_charging_state is None:
|
elif battery_charging_state is None:
|
||||||
battery_charging_state = state.attributes.get(ATTR_BATTERY_CHARGING)
|
battery_charging_state = state.attributes.get(ATTR_BATTERY_CHARGING)
|
||||||
|
@ -201,8 +203,9 @@ class HomeAccessory(Accessory):
|
||||||
self.update_battery, battery_state, battery_charging_state
|
self.update_battery, battery_state, battery_charging_state
|
||||||
)
|
)
|
||||||
|
|
||||||
@ha_callback
|
async def async_update_state_callback(
|
||||||
def update_state_callback(self, entity_id=None, old_state=None, new_state=None):
|
self, entity_id=None, old_state=None, new_state=None
|
||||||
|
):
|
||||||
"""Handle state change listener callback."""
|
"""Handle state change listener callback."""
|
||||||
_LOGGER.debug("New_state: %s", new_state)
|
_LOGGER.debug("New_state: %s", new_state)
|
||||||
if new_state is None:
|
if new_state is None:
|
||||||
|
@ -220,28 +223,28 @@ class HomeAccessory(Accessory):
|
||||||
):
|
):
|
||||||
battery_charging_state = new_state.attributes.get(ATTR_BATTERY_CHARGING)
|
battery_charging_state = new_state.attributes.get(ATTR_BATTERY_CHARGING)
|
||||||
if battery_state is not None or battery_charging_state is not None:
|
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.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
|
async def async_update_linked_battery(
|
||||||
def update_linked_battery(self, entity_id=None, old_state=None, new_state=None):
|
self, entity_id=None, old_state=None, new_state=None
|
||||||
|
):
|
||||||
"""Handle linked battery sensor state change listener callback."""
|
"""Handle linked battery sensor state change listener callback."""
|
||||||
if self.linked_battery_charging_sensor:
|
if self.linked_battery_charging_sensor:
|
||||||
battery_charging_state = None
|
battery_charging_state = None
|
||||||
else:
|
else:
|
||||||
battery_charging_state = new_state.attributes.get(ATTR_BATTERY_CHARGING)
|
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,
|
self.update_battery, new_state.state, battery_charging_state,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ha_callback
|
async def async_update_linked_battery_charging(
|
||||||
def update_linked_battery_charging(
|
|
||||||
self, entity_id=None, old_state=None, new_state=None
|
self, entity_id=None, old_state=None, new_state=None
|
||||||
):
|
):
|
||||||
"""Handle linked battery charging sensor state change listener callback."""
|
"""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
|
self.update_battery, None, new_state.state == STATE_ON
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue