1 """Support for HomematicIP Cloud binary sensor."""
3 from __future__
import annotations
7 from homematicip.aio.device
import (
8 AsyncAccelerationSensor,
11 AsyncFullFlushContactInterface,
12 AsyncFullFlushContactInterface6,
13 AsyncMotionDetectorIndoor,
14 AsyncMotionDetectorOutdoor,
15 AsyncMotionDetectorPushButton,
16 AsyncPluggableMainsFailureSurveillance,
17 AsyncPresenceDetectorIndoor,
19 AsyncRotaryHandleSensor,
21 AsyncShutterContactMagnetic,
23 AsyncTiltVibrationSensor,
26 AsyncWeatherSensorPlus,
27 AsyncWeatherSensorPro,
30 from homematicip.aio.group
import AsyncSecurityGroup, AsyncSecurityZoneGroup
31 from homematicip.base.enums
import SmokeDetectorAlarmType, WindowState
34 BinarySensorDeviceClass,
42 from .const
import DOMAIN
43 from .entity
import HomematicipGenericEntity
44 from .hap
import HomematicipHAP
46 ATTR_ACCELERATION_SENSOR_MODE =
"acceleration_sensor_mode"
47 ATTR_ACCELERATION_SENSOR_NEUTRAL_POSITION =
"acceleration_sensor_neutral_position"
48 ATTR_ACCELERATION_SENSOR_SENSITIVITY =
"acceleration_sensor_sensitivity"
49 ATTR_ACCELERATION_SENSOR_TRIGGER_ANGLE =
"acceleration_sensor_trigger_angle"
50 ATTR_INTRUSION_ALARM =
"intrusion_alarm"
51 ATTR_MOISTURE_DETECTED =
"moisture_detected"
52 ATTR_MOTION_DETECTED =
"motion_detected"
53 ATTR_POWER_MAINS_FAILURE =
"power_mains_failure"
54 ATTR_PRESENCE_DETECTED =
"presence_detected"
55 ATTR_SMOKE_DETECTOR_ALARM =
"smoke_detector_alarm"
56 ATTR_TODAY_SUNSHINE_DURATION =
"today_sunshine_duration_in_minutes"
57 ATTR_WATER_LEVEL_DETECTED =
"water_level_detected"
58 ATTR_WINDOW_STATE =
"window_state"
61 "moistureDetected": ATTR_MOISTURE_DETECTED,
62 "motionDetected": ATTR_MOTION_DETECTED,
63 "powerMainsFailure": ATTR_POWER_MAINS_FAILURE,
64 "presenceDetected": ATTR_PRESENCE_DETECTED,
65 "waterlevelDetected": ATTR_WATER_LEVEL_DETECTED,
68 SAM_DEVICE_ATTRIBUTES = {
69 "accelerationSensorNeutralPosition": ATTR_ACCELERATION_SENSOR_NEUTRAL_POSITION,
70 "accelerationSensorMode": ATTR_ACCELERATION_SENSOR_MODE,
71 "accelerationSensorSensitivity": ATTR_ACCELERATION_SENSOR_SENSITIVITY,
72 "accelerationSensorTriggerAngle": ATTR_ACCELERATION_SENSOR_TRIGGER_ANGLE,
78 config_entry: ConfigEntry,
79 async_add_entities: AddEntitiesCallback,
81 """Set up the HomematicIP Cloud binary sensor from a config entry."""
82 hap = hass.data[DOMAIN][config_entry.unique_id]
84 for device
in hap.home.devices:
85 if isinstance(device, AsyncAccelerationSensor):
87 if isinstance(device, AsyncTiltVibrationSensor):
89 if isinstance(device, AsyncWiredInput32):
92 for channel
in range(1, 33)
94 elif isinstance(device, AsyncFullFlushContactInterface6):
97 for channel
in range(1, 7)
100 device, (AsyncContactInterface, AsyncFullFlushContactInterface)
105 (AsyncShutterContact, AsyncShutterContactMagnetic),
108 if isinstance(device, AsyncRotaryHandleSensor):
113 AsyncMotionDetectorIndoor,
114 AsyncMotionDetectorOutdoor,
115 AsyncMotionDetectorPushButton,
119 if isinstance(device, AsyncPluggableMainsFailureSurveillance):
123 if isinstance(device, AsyncPresenceDetectorIndoor):
125 if isinstance(device, AsyncSmokeDetector):
127 if isinstance(device, AsyncWaterSensor):
130 device, (AsyncRainSensor, AsyncWeatherSensorPlus, AsyncWeatherSensorPro)
134 device, (AsyncWeatherSensor, AsyncWeatherSensorPlus, AsyncWeatherSensorPro)
138 if isinstance(device, AsyncDevice)
and device.lowBat
is not None:
141 for group
in hap.home.groups:
142 if isinstance(group, AsyncSecurityGroup):
144 elif isinstance(group, AsyncSecurityZoneGroup):
151 """Representation of the HomematicIP cloud connection sensor."""
154 """Initialize the cloud connection sensor."""
159 """Return the name cloud connection entity."""
161 name =
"Cloud Connection"
163 return name
if not self._home.name
else f
"{self._home.name} {name}"
167 """Return device specific attributes."""
172 (DOMAIN, self._home.id)
178 """Return the icon of the access point entity."""
180 "mdi:access-point-network"
181 if self._home.connected
182 else "mdi:access-point-network-off"
187 """Return true if hap is connected to cloud."""
188 return self._home.connected
192 """Sensor is always available."""
197 """Representation of the HomematicIP base action sensor."""
199 _attr_device_class = BinarySensorDeviceClass.MOVING
203 """Return true if acceleration is detected."""
204 return self.
_device_device.accelerationSensorTriggered
208 """Return the state attributes of the acceleration sensor."""
209 state_attr = super().extra_state_attributes
211 for attr, attr_key
in SAM_DEVICE_ATTRIBUTES.items():
212 if attr_value := getattr(self.
_device_device, attr,
None):
213 state_attr[attr_key] = attr_value
219 """Representation of the HomematicIP acceleration sensor."""
222 class HomematicipTiltVibrationSensor(HomematicipBaseActionSensor):
223 """Representation of the HomematicIP tilt vibration sensor."""
227 """Representation of the HomematicIP multi room/area contact interface."""
229 _attr_device_class = BinarySensorDeviceClass.OPENING
236 is_multi_channel=
True,
238 """Initialize the multi contact entity."""
240 hap, device, channel=channel, is_multi_channel=is_multi_channel
245 """Return true if the contact interface is on/open."""
246 if self.
_device_device.functionalChannels[self.
_channel_channel].windowState
is None:
249 self.
_device_device.functionalChannels[self.
_channel_channel].windowState
250 != WindowState.CLOSED
255 """Representation of the HomematicIP contact interface."""
257 def __init__(self, hap: HomematicipHAP, device) ->
None:
258 """Initialize the multi contact entity."""
259 super().
__init__(hap, device, is_multi_channel=
False)
263 """Representation of the HomematicIP shutter contact."""
265 _attr_device_class = BinarySensorDeviceClass.DOOR
268 self, hap: HomematicipHAP, device, has_additional_state: bool =
False
270 """Initialize the shutter contact."""
271 super().
__init__(hap, device, is_multi_channel=
False)
276 """Return the state attributes of the Shutter Contact."""
277 state_attr = super().extra_state_attributes
280 window_state = getattr(self.
_device_device,
"windowState",
None)
281 if window_state
and window_state != WindowState.CLOSED:
282 state_attr[ATTR_WINDOW_STATE] = window_state
288 """Representation of the HomematicIP motion detector."""
290 _attr_device_class = BinarySensorDeviceClass.MOTION
294 """Return true if motion is detected."""
295 return self.
_device_device.motionDetected
299 """Representation of the HomematicIP presence detector."""
301 _attr_device_class = BinarySensorDeviceClass.PRESENCE
305 """Return true if presence is detected."""
306 return self.
_device_device.presenceDetected
310 """Representation of the HomematicIP smoke detector."""
312 _attr_device_class = BinarySensorDeviceClass.SMOKE
316 """Return true if smoke is detected."""
317 if self.
_device_device.smokeDetectorAlarmType:
319 self.
_device_device.smokeDetectorAlarmType
320 == SmokeDetectorAlarmType.PRIMARY_ALARM
326 """Representation of the HomematicIP water detector."""
328 _attr_device_class = BinarySensorDeviceClass.MOISTURE
332 """Return true, if moisture or waterlevel is detected."""
333 return self.
_device_device.moistureDetected
or self.
_device_device.waterlevelDetected
337 """Representation of the HomematicIP storm sensor."""
339 def __init__(self, hap: HomematicipHAP, device) ->
None:
340 """Initialize storm sensor."""
341 super().
__init__(hap, device,
"Storm")
345 """Return the icon."""
346 return "mdi:weather-windy" if self.
is_onis_on
else "mdi:pinwheel-outline"
350 """Return true, if storm is detected."""
351 return self.
_device_device.storm
355 """Representation of the HomematicIP rain sensor."""
357 _attr_device_class = BinarySensorDeviceClass.MOISTURE
359 def __init__(self, hap: HomematicipHAP, device) ->
None:
360 """Initialize rain sensor."""
361 super().
__init__(hap, device,
"Raining")
365 """Return true, if it is raining."""
366 return self.
_device_device.raining
370 """Representation of the HomematicIP sunshine sensor."""
372 _attr_device_class = BinarySensorDeviceClass.LIGHT
374 def __init__(self, hap: HomematicipHAP, device) ->
None:
375 """Initialize sunshine sensor."""
376 super().
__init__(hap, device, post=
"Sunshine")
380 """Return true if sun is shining."""
381 return self.
_device_device.sunshine
385 """Return the state attributes of the illuminance sensor."""
386 state_attr = super().extra_state_attributes
388 today_sunshine_duration = getattr(self.
_device_device,
"todaySunshineDuration",
None)
389 if today_sunshine_duration:
390 state_attr[ATTR_TODAY_SUNSHINE_DURATION] = today_sunshine_duration
396 """Representation of the HomematicIP low battery sensor."""
398 _attr_device_class = BinarySensorDeviceClass.BATTERY
400 def __init__(self, hap: HomematicipHAP, device) ->
None:
401 """Initialize battery sensor."""
402 super().
__init__(hap, device, post=
"Battery")
406 """Return true if battery is low."""
407 return self.
_device_device.lowBat
411 HomematicipGenericEntity, BinarySensorEntity
413 """Representation of the HomematicIP pluggable mains failure surveillance sensor."""
415 _attr_device_class = BinarySensorDeviceClass.POWER
417 def __init__(self, hap: HomematicipHAP, device) ->
None:
418 """Initialize pluggable mains failure surveillance sensor."""
423 """Return true if power mains fails."""
424 return not self.
_device_device.powerMainsFailure
428 """Representation of the HomematicIP security zone sensor group."""
430 _attr_device_class = BinarySensorDeviceClass.SAFETY
432 def __init__(self, hap: HomematicipHAP, device, post: str =
"SecurityZone") ->
None:
433 """Initialize security zone group."""
434 device.modelType = f
"HmIP-{post}"
435 super().
__init__(hap, device, post=post)
439 """Security-Group available."""
446 """Return the state attributes of the security zone group."""
447 state_attr = super().extra_state_attributes
449 for attr, attr_key
in GROUP_ATTRIBUTES.items():
450 if attr_value := getattr(self.
_device_device, attr,
None):
451 state_attr[attr_key] = attr_value
453 window_state = getattr(self.
_device_device,
"windowState",
None)
454 if window_state
and window_state != WindowState.CLOSED:
455 state_attr[ATTR_WINDOW_STATE] =
str(window_state)
461 """Return true if security issue detected."""
463 self.
_device_device.motionDetected
464 or self.
_device_device.presenceDetected
466 or self.
_device_device.sabotage
471 self.
_device_device.windowState
is not None
472 and self.
_device_device.windowState != WindowState.CLOSED
479 HomematicipSecurityZoneSensorGroup, BinarySensorEntity
481 """Representation of the HomematicIP security group."""
483 def __init__(self, hap: HomematicipHAP, device) ->
None:
484 """Initialize security group."""
485 super().
__init__(hap, device, post=
"Sensors")
489 """Return the state attributes of the security group."""
490 state_attr = super().extra_state_attributes
492 smoke_detector_at = getattr(self.
_device_device,
"smokeDetectorAlarmType",
None)
493 if smoke_detector_at:
494 if smoke_detector_at == SmokeDetectorAlarmType.PRIMARY_ALARM:
495 state_attr[ATTR_SMOKE_DETECTOR_ALARM] =
str(smoke_detector_at)
496 if smoke_detector_at == SmokeDetectorAlarmType.INTRUSION_ALARM:
497 state_attr[ATTR_INTRUSION_ALARM] =
str(smoke_detector_at)
502 """Return true if safety issue detected."""
508 self.
_device_device.powerMainsFailure
509 or self.
_device_device.moistureDetected
510 or self.
_device_device.waterlevelDetected
512 or self.
_device_device.dutyCycle
517 self.
_device_device.smokeDetectorAlarmType
is not None
518 and self.
_device_device.smokeDetectorAlarmType != SmokeDetectorAlarmType.IDLE_OFF
dict[str, Any] extra_state_attributes(self)
None __init__(self, HomematicipHAP hap, device)
None __init__(self, HomematicipHAP hap)
DeviceInfo device_info(self)
None __init__(self, HomematicipHAP hap, device)
None __init__(self, HomematicipHAP hap, device)
dict[str, Any] extra_state_attributes(self)
None __init__(self, HomematicipHAP hap, device)
None __init__(self, HomematicipHAP hap, device, str post="SecurityZone")
dict[str, Any] extra_state_attributes(self)
None __init__(self, HomematicipHAP hap, device)
dict[str, Any] extra_state_attributes(self)
None __init__(self, HomematicipHAP hap, device)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)