-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
209 lines (192 loc) · 6.65 KB
/
Copy pathbutton.py
File metadata and controls
209 lines (192 loc) · 6.65 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Button entities for IR remote commands and serial presets."""
from __future__ import annotations
import logging
from typing import Any
from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
CONF_CMD_DATA,
CONF_CMD_FORMAT,
CONF_CMD_NAME,
CONF_COMMANDS,
CONF_CONN_PORT,
CONF_IR_COUNT,
CONF_MODULE,
CONF_REMOTE_ID,
CONF_REMOTE_NAME,
CONF_REMOTES,
CONF_SERIAL_COMMANDS,
CONF_SERIAL_ID,
CONF_SERIAL_NAME,
CONF_SERIAL_PAYLOAD,
CONF_SERIAL_PORTS,
DOMAIN,
MANUFACTURER,
)
from .coordinator import ItachCoordinator
from .device_util import (
gateway_device_identifiers,
gateway_via_device,
remote_device_identifiers,
)
from .entity_registry_util import (
active_remote_button_unique_ids,
active_serial_button_unique_ids,
async_remove_stale_entities,
remote_button_unique_id,
serial_button_unique_id,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
coordinator: ItachCoordinator = hass.data[DOMAIN][entry.entry_id]
active = active_serial_button_unique_ids(entry) | active_remote_button_unique_ids(
entry
)
async_remove_stale_entities(hass, entry, "button", active)
entities: list[ButtonEntity] = []
for spec in entry.options.get(CONF_REMOTES, []):
remote_id = str(spec.get(CONF_REMOTE_ID, "")).strip()
if not remote_id:
continue
mod = int(spec[CONF_MODULE])
port = int(spec[CONF_CONN_PORT])
base_repeat = int(spec.get(CONF_IR_COUNT, 1))
for cmd in spec.get(CONF_COMMANDS, []):
name = str(cmd.get(CONF_CMD_NAME, "")).strip()
data = str(cmd.get(CONF_CMD_DATA, "")).strip()
if not name or not data:
continue
entities.append(
ItachRemoteCommandButton(
coordinator,
entry,
spec,
remote_id,
mod,
port,
base_repeat,
name,
cmd,
)
)
for spec in entry.options.get(CONF_SERIAL_PORTS, []):
port_name = str(spec.get(CONF_SERIAL_NAME, "Serial"))
sid = spec[CONF_SERIAL_ID]
mod = int(spec[CONF_MODULE])
port = int(spec[CONF_CONN_PORT])
for cmd in spec.get(CONF_SERIAL_COMMANDS, []):
name = str(cmd.get(CONF_CMD_NAME, "")).strip()
payload = str(cmd.get(CONF_SERIAL_PAYLOAD, "")).strip()
if not name or not payload:
continue
entities.append(
ItachSerialButton(
coordinator,
entry,
spec,
sid,
port_name,
mod,
port,
name,
payload,
)
)
async_add_entities(entities)
class ItachRemoteCommandButton(CoordinatorEntity[ItachCoordinator], ButtonEntity):
"""Send one configured IR command (replaces activity dropdown on device page)."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: ItachCoordinator,
entry: ConfigEntry,
spec: dict[str, Any],
remote_id: str,
module: int,
port: int,
base_repeat: int,
command_name: str,
cmd: dict[str, Any],
) -> None:
super().__init__(coordinator)
self._spec = spec
self._module = module
self._port = port
self._base_repeat = base_repeat
self._command_name = command_name
self._cmd = cmd
self._attr_unique_id = remote_button_unique_id(
entry.entry_id, remote_id, command_name
)
self._attr_name = command_name
self._attr_device_info = {
"identifiers": remote_device_identifiers(entry.entry_id, remote_id),
"name": str(spec.get(CONF_REMOTE_NAME, "Remote")),
"manufacturer": MANUFACTURER,
"model": "IR remote",
"via_device": gateway_via_device(entry.entry_id),
}
async def async_press(self) -> None:
_LOGGER.info("IR button pressed: %s", self._command_name)
fmt = str(self._cmd.get(CONF_CMD_FORMAT, "pronto"))
data = str(self._cmd[CONF_CMD_DATA])
freq = self._cmd.get("freq")
offset = self._cmd.get("offset")
cid = self._cmd.get("command_id")
try:
await self.coordinator.async_send_ir_command(
self._module,
self._port,
fmt,
data,
repeat=self._base_repeat,
offset=int(offset) if offset is not None else None,
frequency=int(freq) if freq is not None else None,
command_id=int(cid) if cid is not None else None,
)
except (ServiceValidationError, OSError, TimeoutError) as err:
raise HomeAssistantError(str(err)) from err
class ItachSerialButton(CoordinatorEntity[ItachCoordinator], ButtonEntity):
"""Fire a preconfigured serial payload."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: ItachCoordinator,
entry: ConfigEntry,
spec: dict[str, Any],
serial_id: str,
port_name: str,
module: int,
port: int,
command_name: str,
payload: str,
) -> None:
super().__init__(coordinator)
self._spec = spec
self._payload = payload
self._attr_unique_id = serial_button_unique_id(
entry.entry_id, str(serial_id), command_name
)
self._attr_name = f"{port_name} {command_name}"
self._attr_device_info = {
"identifiers": gateway_device_identifiers(entry.entry_id),
"name": entry.title,
"manufacturer": MANUFACTURER,
"model": entry.data.get("model") or "iTach",
"sw_version": entry.data.get("firmware") or "",
}
self._attr_extra_state_attributes = {
"module": module,
"port": port,
"payload": payload,
}
async def async_press(self) -> None:
await self.coordinator.async_send_serial(self._spec, self._payload)