def supported_remote_keys(self): """Remote key buttons that are supported.""" if not self.service.has(CharacteristicsTypes.REMOTE_KEY): return frozenset() return clamp_enum_to_char( RemoteKeyValues, self.service[CharacteristicsTypes.REMOTE_KEY])
def enumerate_stateless_switch_group(service): """Enumerate a group of stateless switches, like a remote control.""" switches = list( service.accessory.services.filter( service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH, child_service=service, order_by=[CharacteristicsTypes.SERVICE_LABEL_INDEX], )) results = [] for idx, switch in enumerate(switches): char = switch[CharacteristicsTypes.INPUT_EVENT] # HomeKit itself supports single, double and long presses. But the # manufacturer might not - clamp options to what they say. all_values = clamp_enum_to_char(InputEventValues, char) for event_type in all_values: results.append({ "characteristic": char.iid, "value": event_type, "type": f"button{idx + 1}", "subtype": HK_TO_HA_INPUT_EVENT_VALUES[event_type], }) return results
def enumerate_stateless_switch(service): """Enumerate a stateless switch, like a single button.""" # A stateless switch that has a SERVICE_LABEL_INDEX is part of a group # And is handled separately if ( service.has(CharacteristicsTypes.SERVICE_LABEL_INDEX) and len(service.linked) > 0 ): return [] char = service[CharacteristicsTypes.INPUT_EVENT] # HomeKit itself supports single, double and long presses. But the # manufacturer might not - clamp options to what they say. all_values = clamp_enum_to_char(InputEventValues, char) return [ { "characteristic": char.iid, "value": event_type, "type": "button1", "subtype": HK_TO_HA_INPUT_EVENT_VALUES[event_type], } for event_type in all_values ]
def hvac_modes(self): """Return the list of available hvac operation modes.""" valid_values = clamp_enum_to_char( HeatingCoolingTargetValues, self.service[CharacteristicsTypes.HEATING_COOLING_TARGET], ) return [MODE_HOMEKIT_TO_HASS[mode] for mode in valid_values]
def supported_media_states(self): """Mediate state flags that are supported.""" if not self.service.has(CharacteristicsTypes.TARGET_MEDIA_STATE): return frozenset() return clamp_enum_to_char( TargetMediaStateValues, self.service[CharacteristicsTypes.TARGET_MEDIA_STATE], )
def swing_modes(self): """Return the list of available swing modes. Requires SUPPORT_SWING_MODE. """ valid_values = clamp_enum_to_char( SwingModeValues, self.service[CharacteristicsTypes.SWING_MODE], ) return [SWING_MODE_HOMEKIT_TO_HASS[mode] for mode in valid_values]
def hvac_modes(self): """Return the list of available hvac operation modes.""" valid_values = clamp_enum_to_char( TargetHeaterCoolerStateValues, self.service[CharacteristicsTypes.TARGET_HEATER_COOLER_STATE], ) modes = [ TARGET_HEATER_COOLER_STATE_HOMEKIT_TO_HASS[mode] for mode in valid_values ] modes.append(HVAC_MODE_OFF) return modes
def test_clamp_enum_valid_vals(): a = Accessory() tv_service = a.add_service(service_type=ServicesTypes.TELEVISION) char = tv_service.add_char( CharacteristicsTypes.REMOTE_KEY, valid_values=[RemoteKeyValues.PLAY_PAUSE], min_value=None, max_value=None, ) valid_vals = clamp_enum_to_char(RemoteKeyValues, char) assert valid_vals == {RemoteKeyValues.PLAY_PAUSE}
def _setup_target_media_state(self, char): self._supported_target_media_state = clamp_enum_to_char( TargetMediaStateValues, char) if TargetMediaStateValues.PAUSE in self._supported_target_media_state: self._features |= SUPPORT_PAUSE if TargetMediaStateValues.PLAY in self._supported_target_media_state: self._features |= SUPPORT_PLAY if TargetMediaStateValues.STOP in self._supported_target_media_state: self._features |= SUPPORT_STOP
def test_clamp_enum_min_max_single_press(): a = Accessory() tv_service = a.add_service(service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH) char = tv_service.add_char( CharacteristicsTypes.INPUT_EVENT, valid_values=None, min_value=InputEventValues.SINGLE_PRESS, max_value=InputEventValues.SINGLE_PRESS, ) valid_vals = clamp_enum_to_char(InputEventValues, char) assert valid_vals == {InputEventValues.SINGLE_PRESS}
def test_clamp_enum_min_max_unclamped_button_press(): a = Accessory() tv_service = a.add_service(service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH) char = tv_service.add_char( CharacteristicsTypes.INPUT_EVENT, ) valid_vals = clamp_enum_to_char(InputEventValues, char) assert valid_vals == { InputEventValues.SINGLE_PRESS, InputEventValues.DOUBLE_PRESS, InputEventValues.LONG_PRESS, }
def enumerate_doorbell(service): """Enumerate doorbell buttons.""" input_event = service[CharacteristicsTypes.INPUT_EVENT] # HomeKit itself supports single, double and long presses. But the # manufacturer might not - clamp options to what they say. all_values = clamp_enum_to_char(InputEventValues, input_event) results = [] for event_type in all_values: results.append({ "characteristic": input_event.iid, "value": event_type, "type": "doorbell", "subtype": HK_TO_HA_INPUT_EVENT_VALUES[event_type], }) return results
def _setup_remote_key(self, char): self._supported_remote_key = clamp_enum_to_char(RemoteKeyValues, char) if RemoteKeyValues.PLAY_PAUSE in self._supported_remote_key: self._features |= SUPPORT_PAUSE | SUPPORT_PLAY