Homekit controller BLE groundwork (part 2) (#20548)

* Only fetch values of characteristics we are tracking.

* Use callbacks on subclasses to update individual values

* Update alarm_control_panel to use update callbacks

* Update climate to use update callbacks

* Update cover to use update callbacks

* Update light to use update callbacks

* Update lock to use update callbacks

* Update switch to use update callbacks

* Remove compatibility code as all entities migrated

* pylint by name rather than code
pull/20555/head
Jc2k 2019-01-28 20:27:26 +00:00 committed by Martin Hjelmare
parent 41c1997b88
commit abeb875c61
7 changed files with 76 additions and 112 deletions

View File

@ -247,7 +247,7 @@ class HomeKitEntity(Entity):
setup_fn = getattr(self, '_setup_{}'.format(setup_fn_name), None)
if not setup_fn:
return
# pylint: disable=E1102
# pylint: disable=not-callable
setup_fn(char)
def update(self):
@ -255,19 +255,23 @@ class HomeKitEntity(Entity):
# pylint: disable=import-error
from homekit.exceptions import AccessoryDisconnectedError
pairing = self._accessory.pairing
try:
pairing = self._accessory.pairing
data = pairing.list_accessories_and_characteristics()
new_values_dict = pairing.get_characteristics(self._chars_to_poll)
except AccessoryDisconnectedError:
return
for accessory in data:
if accessory['aid'] != self._aid:
for (_, iid), result in new_values_dict.items():
if 'value' not in result:
continue
for service in accessory['services']:
if service['iid'] != self._iid:
continue
self.update_characteristics(service['characteristics'])
break
# Callback to update the entity with this characteristic value
char_name = escape_characteristic_name(self._char_names[iid])
update_fn = getattr(self, '_update_{}'.format(char_name), None)
if not update_fn:
continue
# pylint: disable=not-callable
update_fn(result['value'])
@property
def unique_id(self):
@ -290,7 +294,7 @@ class HomeKitEntity(Entity):
def update_characteristics(self, characteristics):
"""Synchronise a HomeKit device state with Home Assistant."""
raise NotImplementedError
pass
def put_characteristics(self, characteristics):
"""Control a HomeKit device state from Home Assistant."""

View File

@ -64,18 +64,11 @@ class HomeKitAlarmControlPanel(HomeKitEntity, AlarmControlPanel):
CharacteristicsTypes.BATTERY_LEVEL,
]
def update_characteristics(self, characteristics):
"""Synchronise the Alarm Control Panel state with Home Assistant."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
def _update_security_system_state_current(self, value):
self._state = CURRENT_STATE_MAP[value]
for characteristic in characteristics:
ctype = characteristic['type']
ctype = CharacteristicsTypes.get_short(ctype)
if ctype == "security-system-state.current":
self._state = CURRENT_STATE_MAP[characteristic['value']]
elif ctype == "battery-level":
self._battery_level = characteristic['value']
def _update_battery_level(self, value):
self._battery_level = value
@property
def icon(self):

View File

@ -72,23 +72,17 @@ class HomeKitClimateDevice(HomeKitEntity, ClimateDevice):
def _setup_temperature_target(self, characteristic):
self._features |= SUPPORT_TARGET_TEMPERATURE
def update_characteristics(self, characteristics):
"""Synchronise device state with Home Assistant."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
def _update_heating_cooling_current(self, value):
self._state = MODE_HOMEKIT_TO_HASS.get(value)
for characteristic in characteristics:
ctype = CharacteristicsTypes.get_short_uuid(characteristic['type'])
if ctype == CharacteristicsTypes.HEATING_COOLING_CURRENT:
self._state = MODE_HOMEKIT_TO_HASS.get(
characteristic['value'])
if ctype == CharacteristicsTypes.HEATING_COOLING_TARGET:
self._current_mode = MODE_HOMEKIT_TO_HASS.get(
characteristic['value'])
elif ctype == CharacteristicsTypes.TEMPERATURE_CURRENT:
self._current_temp = characteristic['value']
elif ctype == CharacteristicsTypes.TEMPERATURE_TARGET:
self._target_temp = characteristic['value']
def _update_heating_cooling_target(self, value):
self._current_mode = MODE_HOMEKIT_TO_HASS.get(value)
def _update_temperature_current(self, value):
self._current_temp = value
def _update_temperature_target(self, value):
self._target_temp = value
def set_temperature(self, **kwargs):
"""Set new target temperature."""

View File

@ -85,18 +85,14 @@ class HomeKitGarageDoorCover(HomeKitEntity, CoverDevice):
def _setup_name(self, char):
self._name = char['value']
def update_characteristics(self, characteristics):
"""Synchronise the Cover state with Home Assistant."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
def _update_door_state_current(self, value):
self._state = CURRENT_GARAGE_STATE_MAP[value]
for characteristic in characteristics:
ctype = characteristic['type']
ctype = CharacteristicsTypes.get_short(ctype)
if ctype == "door-state.current":
self._state = CURRENT_GARAGE_STATE_MAP[characteristic['value']]
elif ctype == "obstruction-detected":
self._obstruction_detected = characteristic['value']
def _update_obstruction_detected(self, value):
self._obstruction_detected = value
def _update_name(self, value):
self._name = value
@property
def available(self):
@ -187,31 +183,26 @@ class HomeKitWindowCover(HomeKitEntity, CoverDevice):
def _setup_name(self, char):
self._name = char['value']
def update_characteristics(self, characteristics):
"""Synchronise the Cover state with Home Assistant."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
def _update_position_state(self, value):
self._state = CURRENT_WINDOW_STATE_MAP[value]
for characteristic in characteristics:
ctype = characteristic['type']
ctype = CharacteristicsTypes.get_short(ctype)
if ctype == "position.state":
if 'value' in characteristic:
self._state = \
CURRENT_WINDOW_STATE_MAP[characteristic['value']]
elif ctype == "position.current":
self._position = characteristic['value']
elif ctype == "position.hold":
if 'value' in characteristic:
self._hold = characteristic['value']
elif ctype == "vertical-tilt.current":
if characteristic['value'] is not None:
self._tilt_position = characteristic['value']
elif ctype == "horizontal-tilt.current":
if characteristic['value'] is not None:
self._tilt_position = characteristic['value']
elif ctype == "obstruction-detected":
self._obstruction_detected = characteristic['value']
def _update_position_current(self, value):
self._position = value
def _update_position_hold(self, value):
self._hold = value
def _update_vertical_tilt_current(self, value):
self._tilt_position = value
def _update_horizontal_tilt_current(self, value):
self._tilt_position = value
def _update_obstruction_detected(self, value):
self._obstruction_detected = value
def _update_name(self, value):
self._hold = value
@property
def supported_features(self):

View File

@ -60,24 +60,20 @@ class HomeKitLight(HomeKitEntity, Light):
def _setup_saturation(self, char):
self._features |= SUPPORT_COLOR
def update_characteristics(self, characteristics):
"""Synchronise light state with Home Assistant."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
def _update_on(self, value):
self._on = value
for characteristic in characteristics:
ctype = characteristic['type']
ctype = CharacteristicsTypes.get_short(ctype)
if ctype == "on":
self._on = characteristic['value']
elif ctype == 'brightness':
self._brightness = characteristic['value']
elif ctype == 'color-temperature':
self._color_temperature = characteristic['value']
elif ctype == "hue":
self._hue = characteristic['value']
elif ctype == "saturation":
self._saturation = characteristic['value']
def _update_brightness(self, value):
self._brightness = value
def _update_color_temperature(self, value):
self._color_temperature = value
def _update_hue(self, value):
self._hue = value
def _update_saturation(self, value):
self._saturation = value
@property
def is_on(self):

View File

@ -61,18 +61,11 @@ class HomeKitLock(HomeKitEntity, LockDevice):
CharacteristicsTypes.BATTERY_LEVEL,
]
def update_characteristics(self, characteristics):
"""Synchronise the Lock state with Home Assistant."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
def _update_lock_mechanism_current_state(self, value):
self._state = CURRENT_STATE_MAP[value]
for characteristic in characteristics:
ctype = characteristic['type']
ctype = CharacteristicsTypes.get_short(ctype)
if ctype == "lock-mechanism.current-state":
self._state = CURRENT_STATE_MAP[characteristic['value']]
elif ctype == "battery-level":
self._battery_level = characteristic['value']
def _update_battery_level(self, value):
self._battery_level = value
@property
def name(self):

View File

@ -42,18 +42,11 @@ class HomeKitSwitch(HomeKitEntity, SwitchDevice):
CharacteristicsTypes.OUTLET_IN_USE,
]
def update_characteristics(self, characteristics):
"""Synchronise the switch state with Home Assistant."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
def _update_on(self, value):
self._on = value
for characteristic in characteristics:
ctype = characteristic['type']
ctype = CharacteristicsTypes.get_short(ctype)
if ctype == "on":
self._on = characteristic['value']
elif ctype == "outlet-in-use":
self._outlet_in_use = characteristic['value']
def _update_outlet_in_use(self, value):
self._outlet_in_use = value
@property
def is_on(self):