Refactor homekit_controller entity update to work more like update coordinator (#32670)

* Clean up use of get_characteristic_types

* Get rid of get_hk_char_value helper

* Get rid of _update_fn callbacks

* Call async_write_has_state directly as async_state_changed doesnt do anything any more
This commit is contained in:
Jc2k 2020-03-11 11:40:47 +00:00 committed by GitHub
parent 4248893007
commit 647d137daa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 159 additions and 358 deletions

View file

@ -32,30 +32,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class HomeKitSwitch(HomeKitEntity, SwitchDevice):
"""Representation of a Homekit switch."""
def __init__(self, *args):
"""Initialise the switch."""
super().__init__(*args)
self._on = None
self._outlet_in_use = None
def get_characteristic_types(self):
"""Define the homekit characteristics the entity cares about."""
return [CharacteristicsTypes.ON, CharacteristicsTypes.OUTLET_IN_USE]
def _update_on(self, value):
self._on = value
def _update_outlet_in_use(self, value):
self._outlet_in_use = value
@property
def is_on(self):
"""Return true if device is on."""
return self._on
return self.service.value(CharacteristicsTypes.ON)
async def async_turn_on(self, **kwargs):
"""Turn the specified switch on."""
self._on = True
characteristics = [{"aid": self._aid, "iid": self._chars["on"], "value": True}]
await self._accessory.put_characteristics(characteristics)
@ -67,5 +54,6 @@ class HomeKitSwitch(HomeKitEntity, SwitchDevice):
@property
def device_state_attributes(self):
"""Return the optional state attributes."""
if self._outlet_in_use is not None:
return {OUTLET_IN_USE: self._outlet_in_use}
outlet_in_use = self.service.value(CharacteristicsTypes.OUTLET_IN_USE)
if outlet_in_use is not None:
return {OUTLET_IN_USE: outlet_in_use}