Avoid error when battery appears after homekit has started (#34906)
This commit is contained in:
parent
574d8d30a7
commit
fcd58b7c9b
2 changed files with 33 additions and 4 deletions
|
@ -99,6 +99,9 @@ class HomeAccessory(Accessory):
|
|||
self.entity_id = entity_id
|
||||
self.hass = hass
|
||||
self.debounce = {}
|
||||
self._char_battery = None
|
||||
self._char_charging = None
|
||||
self._char_low_battery = None
|
||||
self.linked_battery_sensor = self.config.get(CONF_LINKED_BATTERY_SENSOR)
|
||||
self.linked_battery_charging_sensor = self.config.get(
|
||||
CONF_LINKED_BATTERY_CHARGING_SENSOR
|
||||
|
@ -247,6 +250,10 @@ class HomeAccessory(Accessory):
|
|||
|
||||
Only call this function if self._support_battery_level is True.
|
||||
"""
|
||||
if not self._char_battery:
|
||||
# Battery appeared after homekit was started
|
||||
return
|
||||
|
||||
battery_level = convert_to_float(battery_level)
|
||||
if battery_level is not None:
|
||||
if self._char_battery.value != battery_level:
|
||||
|
@ -258,7 +265,8 @@ class HomeAccessory(Accessory):
|
|||
"%s: Updated battery level to %d", self.entity_id, battery_level
|
||||
)
|
||||
|
||||
if battery_charging is None:
|
||||
# Charging state can appear after homekit was started
|
||||
if battery_charging is None or not self._char_charging:
|
||||
return
|
||||
|
||||
hk_charging = HK_CHARGING if battery_charging else HK_NOT_CHARGING
|
||||
|
|
|
@ -363,9 +363,30 @@ async def test_missing_linked_battery_sensor(hass, hk_driver, caplog):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert not acc.linked_battery_sensor
|
||||
assert not hasattr(acc, "_char_battery")
|
||||
assert not hasattr(acc, "_char_low_battery")
|
||||
assert not hasattr(acc, "_char_charging")
|
||||
assert acc._char_battery is None
|
||||
assert acc._char_low_battery is None
|
||||
assert acc._char_charging is None
|
||||
|
||||
|
||||
async def test_battery_appears_after_startup(hass, hk_driver, caplog):
|
||||
"""Test battery level appears after homekit is started."""
|
||||
entity_id = "homekit.accessory"
|
||||
hass.states.async_set(entity_id, None, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
acc = HomeAccessory(
|
||||
hass, hk_driver, "Accessory without battery", entity_id, 2, None
|
||||
)
|
||||
acc.update_state = lambda x: None
|
||||
assert acc._char_battery is None
|
||||
|
||||
await acc.run_handler()
|
||||
await hass.async_block_till_done()
|
||||
assert acc._char_battery is None
|
||||
|
||||
hass.states.async_set(entity_id, None, {ATTR_BATTERY_LEVEL: 15})
|
||||
await hass.async_block_till_done()
|
||||
assert acc._char_battery is None
|
||||
|
||||
|
||||
async def test_call_service(hass, hk_driver, events):
|
||||
|
|
Loading…
Add table
Reference in a new issue