diff --git a/homeassistant/components/homekit_controller/__init__.py b/homeassistant/components/homekit_controller/__init__.py index 2c59f062bcc..b5577119178 100644 --- a/homeassistant/components/homekit_controller/__init__.py +++ b/homeassistant/components/homekit_controller/__init__.py @@ -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.""" diff --git a/homeassistant/components/homekit_controller/alarm_control_panel.py b/homeassistant/components/homekit_controller/alarm_control_panel.py index 984be8e0c3b..3a2e5170453 100644 --- a/homeassistant/components/homekit_controller/alarm_control_panel.py +++ b/homeassistant/components/homekit_controller/alarm_control_panel.py @@ -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): diff --git a/homeassistant/components/homekit_controller/climate.py b/homeassistant/components/homekit_controller/climate.py index 042a499c55a..15378e2b046 100644 --- a/homeassistant/components/homekit_controller/climate.py +++ b/homeassistant/components/homekit_controller/climate.py @@ -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.""" diff --git a/homeassistant/components/homekit_controller/cover.py b/homeassistant/components/homekit_controller/cover.py index f9cc2ce435b..c8f087254bb 100644 --- a/homeassistant/components/homekit_controller/cover.py +++ b/homeassistant/components/homekit_controller/cover.py @@ -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): diff --git a/homeassistant/components/homekit_controller/light.py b/homeassistant/components/homekit_controller/light.py index 940e3782379..74ef8948f45 100644 --- a/homeassistant/components/homekit_controller/light.py +++ b/homeassistant/components/homekit_controller/light.py @@ -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): diff --git a/homeassistant/components/homekit_controller/lock.py b/homeassistant/components/homekit_controller/lock.py index 53c94317c69..e27ed444528 100644 --- a/homeassistant/components/homekit_controller/lock.py +++ b/homeassistant/components/homekit_controller/lock.py @@ -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): diff --git a/homeassistant/components/homekit_controller/switch.py b/homeassistant/components/homekit_controller/switch.py index 4bee51803f3..ba4a04022f0 100644 --- a/homeassistant/components/homekit_controller/switch.py +++ b/homeassistant/components/homekit_controller/switch.py @@ -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):