-
Notifications
You must be signed in to change notification settings - Fork 3
/
siren.py
153 lines (132 loc) · 4.44 KB
/
siren.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""The Elro Connects siren platform."""
from __future__ import annotations
from dataclasses import dataclass
import logging
from elro.command import SILENCE_ALARM, TEST_ALARM, TEST_ALARM_ALT, CommandAttributes
from elro.device import (
ALARM_CO,
ALARM_FIRE,
ALARM_HEAT,
ALARM_SMOKE,
ALARM_WATER,
ATTR_DEVICE_STATE,
STATE_SILENCE,
STATE_TEST_ALARM,
STATES_OFFLINE,
STATES_ON,
)
from homeassistant.components.siren import SirenEntity, SirenEntityDescription
from homeassistant.components.siren.const import SirenEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .device import ElroConnectsEntity, ElroConnectsK1
from .helpers import async_set_up_discovery_helper
@dataclass
class ElroSirenEntityDescription(SirenEntityDescription):
"""A class that describes elro siren entities."""
test_alarm: CommandAttributes | None = None
silence_alarm: CommandAttributes | None = None
_LOGGER = logging.getLogger(__name__)
SIREN_DEVICE_TYPES = {
ALARM_CO: ElroSirenEntityDescription(
key=ALARM_CO,
device_class="carbon_monoxide",
name="CO Alarm",
icon="mdi:molecule-co",
test_alarm=TEST_ALARM_ALT,
silence_alarm=SILENCE_ALARM,
),
ALARM_FIRE: ElroSirenEntityDescription(
key=ALARM_FIRE,
device_class="smoke",
name="Fire Alarm",
icon="mdi:fire-alert",
test_alarm=TEST_ALARM,
silence_alarm=SILENCE_ALARM,
),
ALARM_HEAT: ElroSirenEntityDescription(
key=ALARM_HEAT,
device_class="heat",
name="Heat Alarm",
icon="mdi:fire-alert",
test_alarm=TEST_ALARM_ALT,
silence_alarm=SILENCE_ALARM,
),
ALARM_SMOKE: ElroSirenEntityDescription(
key=ALARM_SMOKE,
device_class="smoke",
name="Smoke Alarm",
icon="mdi:smoke",
test_alarm=TEST_ALARM,
silence_alarm=SILENCE_ALARM,
),
ALARM_WATER: ElroSirenEntityDescription(
key=ALARM_WATER,
device_class="moisture",
name="Water Alarm",
icon="mdi:water-alert",
test_alarm=TEST_ALARM_ALT,
silence_alarm=SILENCE_ALARM,
),
}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the sensor platform."""
current: set[int] = set()
async_set_up_discovery_helper(
hass,
ElroConnectsSiren,
config_entry,
current,
SIREN_DEVICE_TYPES,
async_add_entities,
)
class ElroConnectsSiren(ElroConnectsEntity, SirenEntity):
"""Elro Connects Fire Alarm Entity."""
def __init__(
self,
elro_connects_api: ElroConnectsK1,
entry: ConfigEntry,
device_id: int,
description: ElroSirenEntityDescription,
) -> None:
"""Initialize a Fire Alarm Entity."""
self._device_id = device_id
self._elro_connects_api = elro_connects_api
self._description = description
self._attr_supported_features = (
SirenEntityFeature.TURN_ON | SirenEntityFeature.TURN_OFF
)
ElroConnectsEntity.__init__(
self,
elro_connects_api,
entry,
device_id,
description,
)
@property
def is_on(self) -> bool | None:
"""Return true if device is on or none if the device is offline."""
if not self.data or self.data[ATTR_DEVICE_STATE] in STATES_OFFLINE:
return None
return self.data[ATTR_DEVICE_STATE] in STATES_ON
async def async_turn_on(self, **kwargs) -> None:
"""Send a test alarm request."""
_LOGGER.debug("Sending test alarm request for entity %s", self.entity_id)
await self._elro_connects_api.async_command(
self._description.test_alarm, device_ID=self._device_id
)
self.data[ATTR_DEVICE_STATE] = STATE_TEST_ALARM
self.async_write_ha_state()
async def async_turn_off(self, **kwargs) -> None:
"""Send a silence alarm request."""
_LOGGER.debug("Sending silence alarm request for entity %s", self.entity_id)
await self._elro_connects_api.async_command(
self._description.silence_alarm, device_ID=self._device_id
)
self.data[ATTR_DEVICE_STATE] = STATE_SILENCE
self.async_write_ha_state()