1 """Support for Homekit motion sensors."""
3 from __future__
import annotations
5 from aiohomekit.model.characteristics
import CharacteristicsTypes
6 from aiohomekit.model.services
import Service, ServicesTypes
9 BinarySensorDeviceClass,
17 from .
import KNOWN_DEVICES
18 from .connection
import HKDevice
19 from .entity
import HomeKitEntity
23 """Representation of a Homekit motion sensor."""
25 _attr_device_class = BinarySensorDeviceClass.MOTION
28 """Define the homekit characteristics the entity is tracking."""
29 return [CharacteristicsTypes.MOTION_DETECTED]
33 """Has motion been detected."""
34 return self.
serviceservice.value(CharacteristicsTypes.MOTION_DETECTED)
is True
38 """Representation of a Homekit contact sensor."""
40 _attr_device_class = BinarySensorDeviceClass.OPENING
43 """Define the homekit characteristics the entity is tracking."""
44 return [CharacteristicsTypes.CONTACT_STATE]
48 """Return true if the binary sensor is on/open."""
49 return self.
serviceservice.value(CharacteristicsTypes.CONTACT_STATE) == 1
53 """Representation of a Homekit smoke sensor."""
55 _attr_device_class = BinarySensorDeviceClass.SMOKE
58 """Define the homekit characteristics the entity is tracking."""
59 return [CharacteristicsTypes.SMOKE_DETECTED]
63 """Return true if smoke is currently detected."""
64 return self.
serviceservice.value(CharacteristicsTypes.SMOKE_DETECTED) == 1
68 """Representation of a Homekit BO sensor."""
70 _attr_device_class = BinarySensorDeviceClass.CO
73 """Define the homekit characteristics the entity is tracking."""
74 return [CharacteristicsTypes.CARBON_MONOXIDE_DETECTED]
78 """Return true if CO is currently detected."""
79 return self.
serviceservice.value(CharacteristicsTypes.CARBON_MONOXIDE_DETECTED) == 1
83 """Representation of a Homekit occupancy sensor."""
85 _attr_device_class = BinarySensorDeviceClass.OCCUPANCY
88 """Define the homekit characteristics the entity is tracking."""
89 return [CharacteristicsTypes.OCCUPANCY_DETECTED]
93 """Return true if occupancy is currently detected."""
94 return self.
serviceservice.value(CharacteristicsTypes.OCCUPANCY_DETECTED) == 1
98 """Representation of a Homekit leak sensor."""
100 _attr_device_class = BinarySensorDeviceClass.MOISTURE
103 """Define the homekit characteristics the entity is tracking."""
104 return [CharacteristicsTypes.LEAK_DETECTED]
108 """Return true if a leak is detected from the binary sensor."""
109 return self.
serviceservice.value(CharacteristicsTypes.LEAK_DETECTED) == 1
113 """Representation of a Homekit battery low sensor."""
115 _attr_device_class = BinarySensorDeviceClass.BATTERY
116 _attr_entity_category = EntityCategory.DIAGNOSTIC
119 """Define the homekit characteristics the entity is tracking."""
120 return [CharacteristicsTypes.STATUS_LO_BATT]
124 """Return the name of the sensor."""
126 return f
"{name} Low Battery"
131 """Return true if low battery is detected from the binary sensor."""
132 return self.
serviceservice.value(CharacteristicsTypes.STATUS_LO_BATT) == 1
136 ServicesTypes.MOTION_SENSOR: HomeKitMotionSensor,
137 ServicesTypes.CONTACT_SENSOR: HomeKitContactSensor,
138 ServicesTypes.SMOKE_SENSOR: HomeKitSmokeSensor,
139 ServicesTypes.CARBON_MONOXIDE_SENSOR: HomeKitCarbonMonoxideSensor,
140 ServicesTypes.OCCUPANCY_SENSOR: HomeKitOccupancySensor,
141 ServicesTypes.LEAK_SENSOR: HomeKitLeakSensor,
142 ServicesTypes.BATTERY_SERVICE: HomeKitBatteryLowSensor,
146 REQUIRED_CHAR_BY_TYPE = {
147 ServicesTypes.BATTERY_SERVICE: CharacteristicsTypes.STATUS_LO_BATT,
151 REJECT_CHAR_BY_TYPE = {
152 ServicesTypes.BATTERY_SERVICE: CharacteristicsTypes.BATTERY_LEVEL,
158 config_entry: ConfigEntry,
159 async_add_entities: AddEntitiesCallback,
161 """Set up Homekit lighting."""
162 hkid: str = config_entry.data[
"AccessoryPairingID"]
163 conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
166 def async_add_service(service: Service) -> bool:
167 if not (entity_class := ENTITY_TYPES.get(service.type)):
170 required_char := REQUIRED_CHAR_BY_TYPE.get(service.type)
171 )
and not service.has(required_char):
173 if (reject_char := REJECT_CHAR_BY_TYPE.get(service.type))
and service.has(
177 info = {
"aid": service.accessory.aid,
"iid": service.iid}
178 entity: HomeKitEntity = entity_class(conn, info)
179 conn.async_migrate_unique_id(
180 entity.old_unique_id, entity.unique_id, Platform.BINARY_SENSOR
185 conn.add_listener(async_add_service)
list[str] get_characteristic_types(self)
list[str] get_characteristic_types(self)
list[str] get_characteristic_types(self)
list[str] get_characteristic_types(self)
list[str] get_characteristic_types(self)
list[str] get_characteristic_types(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)