1 """Support for Homekit switches."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
8 from aiohomekit.model.characteristics
import (
14 from aiohomekit.model.services
import Service, ServicesTypes
23 from .
import KNOWN_DEVICES
24 from .connection
import HKDevice
25 from .entity
import CharacteristicEntity, HomeKitEntity
27 OUTLET_IN_USE =
"outlet_in_use"
29 ATTR_IN_USE =
"in_use"
30 ATTR_IS_CONFIGURED =
"is_configured"
31 ATTR_REMAINING_DURATION =
"remaining_duration"
34 @dataclass(frozen=True)
36 """Describes Homekit button."""
38 true_value: bool =
True
39 false_value: bool =
False
42 SWITCH_ENTITIES: dict[str, DeclarativeSwitchEntityDescription] = {
44 key=CharacteristicsTypes.VENDOR_AQARA_PAIRING_MODE,
46 translation_key=
"pairing_mode",
47 entity_category=EntityCategory.CONFIG,
50 key=CharacteristicsTypes.VENDOR_AQARA_E1_PAIRING_MODE,
52 translation_key=
"pairing_mode",
53 entity_category=EntityCategory.CONFIG,
56 key=CharacteristicsTypes.LOCK_PHYSICAL_CONTROLS,
57 name=
"Lock Physical Controls",
58 translation_key=
"lock_physical_controls",
59 entity_category=EntityCategory.CONFIG,
62 key=CharacteristicsTypes.MUTE,
64 translation_key=
"mute",
65 entity_category=EntityCategory.CONFIG,
68 key=CharacteristicsTypes.VENDOR_AIRVERSA_SLEEP_MODE,
70 translation_key=
"sleep_mode",
71 entity_category=EntityCategory.CONFIG,
77 """Representation of a Homekit switch."""
80 """Define the homekit characteristics the entity cares about."""
81 return [CharacteristicsTypes.ON, CharacteristicsTypes.OUTLET_IN_USE]
85 """Return true if device is on."""
86 return self.
serviceservice.value(CharacteristicsTypes.ON)
89 """Turn the specified switch on."""
93 """Turn the specified switch off."""
98 """Return the optional state attributes."""
99 outlet_in_use = self.
serviceservice.value(CharacteristicsTypes.OUTLET_IN_USE)
100 if outlet_in_use
is not None:
101 return {OUTLET_IN_USE: outlet_in_use}
106 """Representation of a Homekit faucet."""
109 """Define the homekit characteristics the entity cares about."""
110 return [CharacteristicsTypes.ACTIVE]
114 """Return true if device is on."""
115 return self.
serviceservice.value(CharacteristicsTypes.ACTIVE)
118 """Turn the specified faucet on."""
122 """Turn the specified faucet off."""
127 """Represents a valve in an irrigation system."""
129 _attr_translation_key =
"valve"
132 """Define the homekit characteristics the entity cares about."""
134 CharacteristicsTypes.ACTIVE,
135 CharacteristicsTypes.IN_USE,
136 CharacteristicsTypes.IS_CONFIGURED,
137 CharacteristicsTypes.REMAINING_DURATION,
141 """Turn the specified valve on."""
145 """Turn the specified valve off."""
150 """Return true if device is on."""
151 return self.
serviceservice.value(CharacteristicsTypes.ACTIVE)
155 """Return the optional state attributes."""
158 in_use = self.
serviceservice.value(CharacteristicsTypes.IN_USE)
159 if in_use
is not None:
160 attrs[ATTR_IN_USE] = in_use == InUseValues.IN_USE
162 is_configured = self.
serviceservice.value(CharacteristicsTypes.IS_CONFIGURED)
163 if is_configured
is not None:
164 attrs[ATTR_IS_CONFIGURED] = is_configured == IsConfiguredValues.CONFIGURED
166 remaining = self.
serviceservice.value(CharacteristicsTypes.REMAINING_DURATION)
167 if remaining
is not None:
168 attrs[ATTR_REMAINING_DURATION] = remaining
174 """Representation of a Homekit switch backed by a single characteristic."""
180 char: Characteristic,
181 description: DeclarativeSwitchEntityDescription,
183 """Initialise a HomeKit switch."""
184 self.entity_description: DeclarativeSwitchEntityDescription = description
189 """Return the name of the device if any."""
191 return f
"{name} {self.entity_description.name}"
192 return f
"{self.entity_description.name}"
195 """Define the homekit characteristics the entity cares about."""
196 return [self.
_char_char.type]
200 """Return true if device is on."""
201 return self.
_char_char.value == self.entity_description.true_value
204 """Turn the specified switch on."""
206 {self.
_char_char.type: self.entity_description.true_value}
210 """Turn the specified switch off."""
212 {self.
_char_char.type: self.entity_description.false_value}
216 ENTITY_TYPES: dict[str, type[HomeKitSwitch | HomeKitFaucet | HomeKitValve]] = {
217 ServicesTypes.SWITCH: HomeKitSwitch,
218 ServicesTypes.OUTLET: HomeKitSwitch,
219 ServicesTypes.FAUCET: HomeKitFaucet,
220 ServicesTypes.VALVE: HomeKitValve,
226 config_entry: ConfigEntry,
227 async_add_entities: AddEntitiesCallback,
229 """Set up Homekit switches."""
230 hkid: str = config_entry.data[
"AccessoryPairingID"]
231 conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
234 def async_add_service(service: Service) -> bool:
235 if not (entity_class := ENTITY_TYPES.get(service.type)):
237 info = {
"aid": service.accessory.aid,
"iid": service.iid}
238 entity: HomeKitSwitch | HomeKitFaucet | HomeKitValve = entity_class(conn, info)
239 conn.async_migrate_unique_id(
240 entity.old_unique_id, entity.unique_id, Platform.SWITCH
245 conn.add_listener(async_add_service)
249 if not (description := SWITCH_ENTITIES.get(char.type)):
252 info = {
"aid": char.service.accessory.aid,
"iid": char.service.iid}
254 conn.async_migrate_unique_id(
255 entity.old_unique_id, entity.unique_id, Platform.SWITCH
260 conn.add_char_factory(async_add_characteristic)
None async_put_characteristics(self, dict[str, Any] characteristics)
None async_turn_on(self, **Any kwargs)
list[str] get_characteristic_types(self)
None __init__(self, HKDevice conn, ConfigType info, Characteristic char, DeclarativeSwitchEntityDescription description)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
list[str] get_characteristic_types(self)
list[str] get_characteristic_types(self)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
dict[str, Any]|None extra_state_attributes(self)
list[str] get_characteristic_types(self)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
dict[str, Any] extra_state_attributes(self)
bool async_add_characteristic(Characteristic char)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)