forked from klyqa/home-assistant-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacoordinator.py
253 lines (222 loc) · 8.06 KB
/
datacoordinator.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Klyqa datacoordinator."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_component import (
EntityComponent,
DEFAULT_SCAN_INTERVAL,
)
from homeassistant.helpers import (
entity_registry as ent_reg,
)
from typing import Any
import socket
from homeassistant.const import Platform
from klyqa_ctl import klyqa_ctl as api
from .const import (
DOMAIN,
LOGGER,
CONF_POLLING,
CONF_SYNC_ROOMS,
EVENT_KLYQA_NEW_LIGHT,
EVENT_KLYQA_NEW_LIGHT_GROUP,
)
from homeassistant.const import (
CONF_PASSWORD,
CONF_HOST,
CONF_SCAN_INTERVAL,
CONF_USERNAME,
)
from datetime import timedelta
import logging
from homeassistant.components.light import ENTITY_ID_FORMAT
from homeassistant.util import slugify
class HAKlyqaAccount(api.Klyqa_account): # type: ignore[misc]
"""HAKlyqaAccount."""
hass: HomeAssistant | None
udp: socket.socket
tcp: socket.socket
polling: bool
sync_rooms: bool
scan_interval_conf: float
def __init__(
self,
udp: Any,
tcp: Any,
username: str = "",
password: str = "",
host: str = "",
hass: HomeAssistant | None = None,
polling: bool = True,
sync_rooms: bool = True,
scan_interval: float = -1.0,
):
"""HAKlyqaAccount."""
super().__init__(username, password, host)
self.hass = hass
self.udp = udp
self.tcp = tcp
self.polling = polling
self.sync_rooms = sync_rooms
self.scan_interval_conf = scan_interval
async def send_to_bulbs(
self,
args_parsed: Any,
args_in: Any,
timeout_ms: Any = 5000,
async_answer_callback: Any = None,
) -> Any:
"""Send_to_bulbs."""
ret = await super()._send_to_bulbs(
args_parsed,
args_in,
self.udp,
self.tcp,
async_answer_callback=async_answer_callback,
timeout_ms=timeout_ms,
)
return ret
# pylint: disable=arguments-differ
async def login(self, print_onboarded_lamps=False) -> bool:
"""Login."""
ret = await super().login(print_onboarded_lamps=False)
if ret:
await api.async_json_cache(
{
CONF_USERNAME: self.username,
CONF_PASSWORD: self.password,
CONF_SCAN_INTERVAL: self.scan_interval_conf,
CONF_SYNC_ROOMS: self.sync_rooms,
CONF_POLLING: self.polling,
CONF_HOST: self.host,
},
"last.klyqa_integration_data.cache.json",
)
return ret
async def update_account(self) -> bool:
"""Update_account."""
await self.request_account_settings()
await self.process_account_settings()
async def process_account_settings(self) -> None:
"""Process_account_settings."""
klyqa_new_light_registered = [
key
for key, _ in self.hass.bus.async_listeners().items()
if key == EVENT_KLYQA_NEW_LIGHT or key == EVENT_KLYQA_NEW_LIGHT_GROUP
]
if len(klyqa_new_light_registered) == 2:
# if EVENT_KLYQA_NEW_LIGHT in self.hass.bus._listeners:
ha_entities = self.hass.data["light"].entities
entity_registry = ent_reg.async_get(self.hass)
for device in self.acc_settings["devices"]:
# look if any onboarded device is not in the entity registry already
u_id = api.format_uid(device["localDeviceId"])
registered_entity_id = entity_registry.async_get_entity_id(
Platform.LIGHT, DOMAIN, u_id
)
# light = [
# entity
# for entity in ha_entities
# if hasattr(entity, "u_id") and entity.u_id == u_id
# ]
if (
# not registered_entity_id
not self.hass.states.get(ENTITY_ID_FORMAT.format(u_id))
): #
# if len(light) == 0:
if device["productId"].startswith("@klyqa.lighting"):
"""found klyqa device not in the light entities"""
self.hass.bus.fire(EVENT_KLYQA_NEW_LIGHT, device)
for group in self.acc_settings["deviceGroups"]:
u_id = api.format_uid(group["id"])
# light = [
# entity
# for entity in ha_entities
# if hasattr(entity, "u_id") and entity.u_id == u_id
# ]
registered_entity_id = entity_registry.async_get_entity_id(
Platform.LIGHT, DOMAIN, u_id
)
# if len(light) == 0:
if (
# not registered_entity_id
not self.hass.states.get(
ENTITY_ID_FORMAT.format(slugify(group["name"]))
)
): # self.hass.states.get(ENTITY_ID_FORMAT.format(u_id)):
"""found klyqa device not in the light entities"""
if (
len(group["devices"]) > 0
and "productId" in group["devices"][0]
and group["devices"][0]["productId"].startswith(
"@klyqa.lighting"
)
):
self.hass.bus.fire(EVENT_KLYQA_NEW_LIGHT_GROUP, group)
return True
class KlyqaDataCoordinator(EntityComponent):
"""KlyqaDataCoordinator."""
_instance = None
klyqa_accounts: dict[str, HAKlyqaAccount]
udp: socket.socket
tcp: socket.socket
remove_listeners: list
# pylint: disable = super-init-not-called
def __init__(self) -> None:
"""KlyqaDataCoordinator."""
raise RuntimeError("Call instance() instead")
def get_ports(self) -> int:
"""Get_ports."""
try:
self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ("0.0.0.0", 2222)
self.udp.bind(server_address)
LOGGER.debug("Bound UDP port 2222")
except: # noqa: E722 pylint: disable=bare-except
LOGGER.error(
"Error on opening and binding the udp port 2222 on host for initiating the lamp communication"
)
return 1
try:
self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ("0.0.0.0", 3333)
self.tcp.bind(server_address)
LOGGER.debug("Bound TCP port 3333")
self.tcp.listen(1)
except: # noqa: E722 pylint: disable=bare-except
LOGGER.error(
"Error on opening and binding the tcp port 3333 on host for initiating the lamp communication"
)
return 1
return 0
def init(
self,
logger: logging.Logger,
domain: str,
hass: HomeAssistant,
scan_interval: timedelta = DEFAULT_SCAN_INTERVAL,
) -> None:
"""__init"""
print("Init new instance")
super().__init__(logger, domain, hass, scan_interval)
self.klyqa_accounts = {}
self.get_ports()
self.entries = {}
self.remove_listeners = []
@classmethod
def instance(
cls,
logger: logging.Logger,
domain: str,
hass: HomeAssistant,
scan_interval: timedelta = DEFAULT_SCAN_INTERVAL,
) -> KlyqaDataCoordinator:
"""instance"""
if cls._instance is None:
print("Creating new instance")
cls._instance = cls.__new__(cls)
# Put any initialization here.
cls._instance.init(logger, domain, hass, scan_interval)
return cls._instance