1 """Support for Hue binary sensors."""
3 from __future__
import annotations
5 from functools
import partial
7 from aiohue.v2
import HueBridgeV2
8 from aiohue.v2.controllers.config
import (
9 EntertainmentConfiguration,
10 EntertainmentConfigurationController,
12 from aiohue.v2.controllers.events
import EventType
13 from aiohue.v2.controllers.sensors
import (
14 CameraMotionController,
19 from aiohue.v2.models.camera_motion
import CameraMotion
20 from aiohue.v2.models.contact
import Contact, ContactState
21 from aiohue.v2.models.entertainment_configuration
import EntertainmentStatus
22 from aiohue.v2.models.motion
import Motion
23 from aiohue.v2.models.tamper
import Tamper, TamperState
26 BinarySensorDeviceClass,
28 BinarySensorEntityDescription,
35 from ..bridge
import HueBridge
36 from ..const
import DOMAIN
37 from .entity
import HueBaseEntity
39 type SensorType = CameraMotion | Contact | Motion | EntertainmentConfiguration | Tamper
40 type ControllerType = (
41 CameraMotionController
44 | EntertainmentConfigurationController
51 config_entry: ConfigEntry,
52 async_add_entities: AddEntitiesCallback,
54 """Set up Hue Sensors from Config Entry."""
55 bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id]
56 api: HueBridgeV2 = bridge.api
59 def register_items(controller: ControllerType, sensor_class: SensorType):
60 make_binary_sensor_entity = partial(sensor_class, bridge, controller)
63 def async_add_sensor(event_type: EventType, resource: SensorType) ->
None:
64 """Add Hue Binary Sensor."""
71 config_entry.async_on_unload(
73 async_add_sensor, event_filter=EventType.RESOURCE_ADDED
78 register_items(api.sensors.camera_motion, HueMotionSensor)
79 register_items(api.sensors.motion, HueMotionSensor)
80 register_items(api.config.entertainment_configuration, HueEntertainmentActiveSensor)
81 register_items(api.sensors.contact, HueContactSensor)
82 register_items(api.sensors.tamper, HueTamperSensor)
87 """Representation of a Hue Motion sensor."""
89 controller: CameraMotionController | MotionController
90 resource: CameraMotion | Motion
94 device_class=BinarySensorDeviceClass.MOTION,
100 """Return true if the binary sensor is on."""
101 if not self.
resourceresource.enabled:
104 return self.
resourceresource.motion.value
109 """Representation of a Hue Entertainment Configuration as binary sensor."""
111 controller: EntertainmentConfigurationController
112 resource: EntertainmentConfiguration
115 key=
"entertainment_active_sensor",
116 device_class=BinarySensorDeviceClass.RUNNING,
117 has_entity_name=
False,
122 """Return true if the binary sensor is on."""
123 return self.
resourceresource.status == EntertainmentStatus.ACTIVE
127 """Return sensor name."""
128 return self.
resourceresource.metadata.name
133 """Representation of a Hue Contact sensor."""
135 controller: ContactController
139 key=
"contact_sensor",
140 device_class=BinarySensorDeviceClass.OPENING,
141 has_entity_name=
True,
146 """Return true if the binary sensor is on."""
147 if not self.
resourceresource.enabled:
150 return self.
resourceresource.contact_report.state != ContactState.CONTACT
155 """Representation of a Hue Tamper sensor."""
157 controller: TamperController
162 device_class=BinarySensorDeviceClass.TAMPER,
163 entity_category=EntityCategory.DIAGNOSTIC,
164 has_entity_name=
True,
169 """Return true if the binary sensor is on."""
170 if not self.
resourceresource.tamper_reports:
172 return self.
resourceresource.tamper_reports[0].state == TamperState.TAMPERED
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)