-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_sensor.py
More file actions
105 lines (83 loc) · 3.54 KB
/
Copy pathbinary_sensor.py
File metadata and controls
105 lines (83 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""Binary sensor platform for Ajax Cloud."""
from __future__ import annotations
from typing import Any
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
ATTR_BATTERY,
ATTR_SIGNAL_STRENGTH,
ATTR_TAMPER,
DEVICE_TYPE_DOOR,
DEVICE_TYPE_FIRE,
DEVICE_TYPE_LEAK,
DEVICE_TYPE_MOTION,
DOMAIN,
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Ajax Cloud binary sensors."""
coordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"]
entities = []
devices = coordinator.data.get("devices", [])
for device in devices:
device_type = device.get("type")
if device_type in [DEVICE_TYPE_MOTION, DEVICE_TYPE_DOOR, DEVICE_TYPE_LEAK, DEVICE_TYPE_FIRE]:
entities.append(AjaxBinarySensor(coordinator, device))
async_add_entities(entities)
class AjaxBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Representation of an Ajax binary sensor."""
_attr_has_entity_name = True
def __init__(self, coordinator, device: dict[str, Any]) -> None:
"""Initialize the binary sensor."""
super().__init__(coordinator)
self._device = device
self._attr_unique_id = f"ajax_{device['type']}_{device['id']}"
self._attr_name = device.get("name", f"Ajax {device['type']}")
# Set device class based on type
device_type = device.get("type")
if device_type == DEVICE_TYPE_MOTION:
self._attr_device_class = BinarySensorDeviceClass.MOTION
elif device_type == DEVICE_TYPE_DOOR:
self._attr_device_class = BinarySensorDeviceClass.DOOR
elif device_type == DEVICE_TYPE_LEAK:
self._attr_device_class = BinarySensorDeviceClass.MOISTURE
elif device_type == DEVICE_TYPE_FIRE:
self._attr_device_class = BinarySensorDeviceClass.SMOKE
@property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
devices = self.coordinator.data.get("devices", [])
device = next((d for d in devices if d["id"] == self._device["id"]), None)
if not device:
return None
return device.get("state", False)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return additional attributes."""
devices = self.coordinator.data.get("devices", [])
device = next((d for d in devices if d["id"] == self._device["id"]), None)
if not device:
return {}
attributes = {}
if ATTR_BATTERY in device:
attributes[ATTR_BATTERY] = device[ATTR_BATTERY]
if ATTR_SIGNAL_STRENGTH in device:
attributes[ATTR_SIGNAL_STRENGTH] = device[ATTR_SIGNAL_STRENGTH]
if ATTR_TAMPER in device:
attributes[ATTR_TAMPER] = device[ATTR_TAMPER]
return attributes
@property
def available(self) -> bool:
"""Return if entity is available."""
devices = self.coordinator.data.get("devices", [])
device = next((d for d in devices if d["id"] == self._device["id"]), None)
return device is not None and device.get("online", False)