-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.py
More file actions
62 lines (51 loc) · 2.05 KB
/
switch.py
File metadata and controls
62 lines (51 loc) · 2.05 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
from __future__ import annotations
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from . import DOMAIN
from .coordinator import ITagClient
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities):
mac = entry.data["mac"].upper()
store = hass.data[DOMAIN]
clients = store.setdefault("clients", {})
client: ITagClient | None = clients.get(mac)
if client is None:
client = clients[mac] = ITagClient(hass, mac)
async_add_entities([
ITagBeepSwitch(mac, client),
ITagLinkAlertSwitch(mac, client),
])
class ITagBeepSwitch(SwitchEntity):
"""Немедленный писк (Immediate Alert / 0x1802:2A06)."""
def __init__(self, mac: str, client: ITagClient):
self._mac = mac
self._client = client
self._state = False
self._attr_name = f"iTag Beep {mac}"
self._attr_unique_id = f"itag_beep_{mac.replace(':','_')}_v2"
async def async_turn_on(self, **kwargs):
await self._client.beep(True)
self._state = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs):
await self._client.beep(False)
self._state = False
self.async_write_ha_state()
@property
def is_on(self) -> bool:
return self._state
class ITagLinkAlertSwitch(SwitchEntity):
"""Писк при потере связи (Link Loss / 0x1803:2A06, write-with-response + readback)."""
def __init__(self, mac: str, client: ITagClient):
self._mac = mac
self._client = client
self._attr_name = f"iTag Link Alert {mac}"
self._attr_unique_id = f"itag_linkalert_{mac.replace(':','_')}_v2"
async def async_turn_on(self, **kwargs):
await self._client.set_link_alert(True)
self.async_write_ha_state()
async def async_turn_off(self, **kwargs):
await self._client.set_link_alert(False)
self.async_write_ha_state()
@property
def is_on(self) -> bool:
return self._client.link_alert_enabled