-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaveo_box.py
More file actions
executable file
·604 lines (508 loc) · 24.2 KB
/
maveo_box.py
File metadata and controls
executable file
·604 lines (508 loc) · 24.2 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
"""Nymea hub communication module using JSON-RPC and WebSocket."""
from __future__ import annotations
import asyncio
from collections.abc import Callable
import contextlib
import json
import logging
import socket
import ssl
from threading import Lock
from typing import Any
from homeassistant.core import HomeAssistant
import websockets
# from .nymea import Nymea
_LOGGER = logging.getLogger(__name__)
mutex: Lock = Lock()
class MaveoBox:
"""Maveo Box."""
def __init__(
self,
hass: HomeAssistant,
host: str,
port: int,
token: str | None = None,
websocket_port: int = 4444,
) -> None:
"""Init maveo box."""
self._host: str = host
self._port: int = port # JSON-RPC port (typically 2222)
self._ws_port: int = (
websocket_port # WebSocket port for notifications (typically 4444)
)
self._hass: HomeAssistant = hass
self._name: str = host
self._id: str = host.lower()
self._token: str | None = token
self._pushButtonAuthAvailable: bool = False
self._authenticationRequired: bool = True
self._initialSetupRequired: bool = False
self._commandId: int = 0
# JSON-RPC socket for commands (port 2222)
self._sock: socket.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# WebSocket connection for notifications (port 4444)
self._ws: Any = None
self._ws_task: asyncio.Task[None] | None = None
self.maveoSticks: list[Any] = []
self.things: list[Any] = []
self.online: bool = True
# Thing classes data for dynamic entity generation
self.thing_classes: list[dict[str, Any]] = []
self.vendors: dict[str, dict[str, Any]] = {}
# Notification system
self._notification_handlers: dict[
str, list[Callable[[dict[str, Any]], None]]
] = {}
self._stop_notification_listener: bool = False
@property
def hub_id(self) -> str:
"""ID for nymea hub."""
return self._id
async def test_connection(self) -> bool:
"""Tests initial connectivity during setup."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(5)
try:
sock.connect((self._host, self._port))
return True # noqa: TRY300
except (TimeoutError, OSError):
return False
def _create_ssl_context(self) -> ssl.SSLContext:
"""Create SSL context with self-signed certificate support."""
context = ssl.create_default_context()
# As we are working with self signed certificates disable some cert checks.
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
return context
async def init_connection(self) -> str | None:
"""Inits the connection to the maveo box and returns a token used for authentication."""
# Run blocking socket operations in executor
loop = self._hass.loop
# Connect to socket in executor (blocking I/O)
await loop.run_in_executor(None, self._sock.connect, (self._host, self._port))
# Perform initial handshake
try:
# first try without ssl
handshake_message = self.send_command("JSONRPC.Hello", {})
except Exception:
# on case of error try with ssl
# Create SSL context in executor to avoid blocking
context = await loop.run_in_executor(None, self._create_ssl_context)
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
await loop.run_in_executor(
None, self._sock.connect, (self._host, self._port)
)
# Wrap socket with SSL in executor
self._sock = await loop.run_in_executor(
None, context.wrap_socket, self._sock
)
handshake_message = self.send_command("JSONRPC.Hello", {})
handshake_data = handshake_message["params"]
self._initialSetupRequired = handshake_data["initialSetupRequired"]
self._authenticationRequired = handshake_data["authenticationRequired"]
self._pushButtonAuthAvailable = handshake_data["pushButtonAuthAvailable"]
# If we don't need any authentication, we are done
if not self._authenticationRequired:
_LOGGER.warning(
"Maveo box is configured to allow unauthenticated requests, skipping authentication"
)
return None
if self._initialSetupRequired:
raise NotImplementedError(
"An uninitialized maveo box is currently not supported"
)
if not self._pushButtonAuthAvailable:
raise NotImplementedError(
"A maveo box without push button is currently not supported"
)
# Authenticate if no token
if self._authenticationRequired and self._token is None:
self._token = self._pushbuttonAuthentication()
# Enable notifications for the Integrations namespace
self._enable_notifications()
# Note: Notification listener will be started later from __init__.py
# to avoid blocking during initialization
return self._token
def _pushbuttonAuthentication(self) -> str | None:
"""Authenticate using push button method."""
if self._token is not None:
return self._token
_LOGGER.info("Using push button authentication method")
params: dict[str, str] = {"deviceName": "home assistant"}
command_obj: dict[str, Any] = {
"id": self._commandId,
"params": params,
"method": "JSONRPC.RequestPushButtonAuth",
}
command = json.dumps(command_obj) + "\n"
self._sock.send(command.encode("utf-8"))
# wait for the response with id = commandId
response_id = -1
while response_id != self._commandId:
data = b""
while b"}\n" not in data:
chunk = self._sock.recv(4096)
if chunk == b"":
raise RuntimeError("socket connection broken")
data += chunk
response = json.loads(data.decode("utf-8"))
response_id = response["id"]
self._commandId = self._commandId + 1
# Check response.
_LOGGER.info(
"Initialized push button authentication, please press the pushbutton on the device"
)
# wait for push button notification
while True:
data = b""
while b"}\n" not in data:
chunk = self._sock.recv(4096)
if chunk == b"":
raise RuntimeError("socket connection broken")
data += chunk
response = json.loads(data.decode("utf-8"))
if ("notification" in response) and response[
"notification"
] == "JSONRPC.PushButtonAuthFinished":
_LOGGER.info("Notification received")
if response["params"]["success"] is True:
_LOGGER.info("Authenticated successfully")
return response["params"]["token"]
def _enable_notifications(self) -> None:
"""Enable notifications for relevant namespaces."""
# In Nymea, notifications are automatically enabled after authentication.
# There's no need to explicitly enable them via API call.
# The notification listener will receive all notifications once started.
_LOGGER.info("Notifications are enabled by default after authentication")
def send_command(
self, method: str, params: dict[str, Any] | None = None
) -> dict[str, Any] | None:
"""Send a command via JSON-RPC socket and wait for response."""
with mutex:
command_obj: dict[str, Any] = {"id": self._commandId, "method": method}
command_id: int = self._commandId
self._commandId += 1
if self._authenticationRequired and self._token is not None:
command_obj["token"] = self._token
if params is not None and len(params) > 0:
command_obj["params"] = params
# Send via JSON-RPC socket (port 2223).
command: str = json.dumps(command_obj) + "\n"
self._sock.send(command.encode("utf-8"))
# Wait for the response with id = commandId.
responseId: int = -1
while responseId != command_id:
data: bytes = b""
while b"}\n" not in data:
chunk: bytes = self._sock.recv(4096)
if chunk == b"":
raise RuntimeError("socket connection broken")
data += chunk
response: dict[str, Any] = json.loads(data.decode("utf-8"))
# Skip notifications (should rarely happen on command socket now).
if "notification" in response:
_LOGGER.warning(
"Received notification on command socket: %s",
response["notification"],
)
continue
responseId = response["id"]
if response["status"] != "success":
_LOGGER.error("JSON error happened: %s", response.get("error"))
return None
# Call went fine, return the response.
return response
async def _websocket_listener(self) -> None:
"""WebSocket listener for push notifications from Nymea (port 4444)."""
_LOGGER.info("Starting WebSocket notification listener")
# Determine if we need SSL.
ws_url: str = f"ws://{self._host}:{self._ws_port}"
ssl_context: ssl.SSLContext | None = None
try:
# Try non-SSL first.
async with websockets.connect(ws_url) as websocket:
await self._ws_listen_loop(websocket)
except (websockets.exceptions.WebSocketException, OSError) as ex:
_LOGGER.info("Non-SSL WebSocket failed, trying SSL: %s", ex)
# Try with SSL - create SSL context in executor to avoid blocking.
loop = self._hass.loop
ssl_context = await loop.run_in_executor(None, self._create_ssl_context)
ws_url = f"wss://{self._host}:{self._ws_port}"
try:
async with websockets.connect(ws_url, ssl=ssl_context) as websocket:
await self._ws_listen_loop(websocket)
except Exception as ex:
_LOGGER.error("Failed to connect WebSocket: %s", ex)
self.online = False
async def _ws_listen_loop(self, websocket: Any) -> None:
"""Main WebSocket listening loop."""
try:
# First, send JSONRPC.Hello handshake on WebSocket (without token).
hello_message: dict[str, Any] = {
"id": 0,
"method": "JSONRPC.Hello",
"params": {},
}
await websocket.send(json.dumps(hello_message))
hello_response: dict[str, Any] = json.loads(await websocket.recv())
if hello_response.get("status") != "success":
_LOGGER.error("WebSocket handshake failed: %s", hello_response)
return
_LOGGER.debug(
"WebSocket handshake successful: %s", hello_response.get("params", {})
)
# If authentication is required, send Hello again WITH the token.
# This authenticates the WebSocket session.
if self._authenticationRequired and self._token:
auth_hello = {
"id": 1,
"method": "JSONRPC.Hello",
"params": {},
"token": self._token,
}
await websocket.send(json.dumps(auth_hello))
auth_response = json.loads(await websocket.recv())
if auth_response.get("status") != "success":
_LOGGER.error(
"WebSocket token authentication failed: %s", auth_response
)
return
_LOGGER.info("WebSocket authenticated with token")
# Now enable notifications (after authentication).
enable_notifications = {
"id": 2,
"method": "JSONRPC.SetNotificationStatus",
"params": {"enabled": True},
}
# Add token at top level if authentication is required.
if self._authenticationRequired and self._token:
enable_notifications["token"] = self._token
await websocket.send(json.dumps(enable_notifications))
notif_response = json.loads(await websocket.recv())
if notif_response.get("status") == "success":
_LOGGER.info("WebSocket notifications enabled")
else:
_LOGGER.warning("Failed to enable notifications: %s", notif_response)
# Listen for notifications.
while not self._stop_notification_listener:
try:
message_str = await asyncio.wait_for(websocket.recv(), timeout=1.0)
message = json.loads(message_str)
# Only process notifications (not command responses).
if "notification" in message:
notification_name = message["notification"]
params = message.get("params", {})
_LOGGER.info(
"WebSocket notification: %s with params: %s",
notification_name,
params,
)
# Dispatch to registered handlers.
if notification_name in self._notification_handlers:
for handler in self._notification_handlers[
notification_name
]:
try:
# Call handler in Home Assistant's event loop.
self._hass.loop.call_soon_threadsafe(
handler, params
)
except Exception as ex:
_LOGGER.error(
"Error calling notification handler: %s", ex
)
else:
_LOGGER.debug(
"No handler registered for: %s", notification_name
)
else:
# Command response on WebSocket (shouldn't happen often).
_LOGGER.debug(
"Received command response on WebSocket: %s", message
)
except TimeoutError:
# Normal timeout, continue loop.
continue
except websockets.exceptions.ConnectionClosed:
_LOGGER.warning("WebSocket connection closed")
break
except Exception as ex:
_LOGGER.exception("Error in WebSocket listener: %s", ex)
break
except Exception as ex:
_LOGGER.exception("WebSocket listen loop error: %s", ex)
finally:
_LOGGER.info("WebSocket notification listener stopped")
def register_notification_handler(
self, notification_name: str, handler: Callable[[dict[str, Any]], None]
) -> None:
"""Register a handler for a specific notification type."""
if notification_name not in self._notification_handlers:
self._notification_handlers[notification_name] = []
self._notification_handlers[notification_name].append(handler)
_LOGGER.debug("Registered handler for notification: %s", notification_name)
def unregister_notification_handler(
self, notification_name: str, handler: Callable[[dict[str, Any]], None]
) -> None:
"""Unregister a notification handler."""
if notification_name in self._notification_handlers:
self._notification_handlers[notification_name].remove(handler)
if not self._notification_handlers[notification_name]:
del self._notification_handlers[notification_name]
_LOGGER.debug(
"Unregistered handler for notification: %s", notification_name
)
def start_notification_listener(self) -> None:
"""Start the WebSocket notification listener."""
if self._ws_task is None or self._ws_task.done():
self._stop_notification_listener = False
self._ws_task = self._hass.async_create_task(self._websocket_listener())
_LOGGER.info("Started WebSocket notification listener task")
async def stop_notification_listener(self) -> None:
"""Stop the WebSocket notification listener."""
self._stop_notification_listener = True
if self._ws_task and not self._ws_task.done():
self._ws_task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await self._ws_task
_LOGGER.info("Stopped WebSocket notification listener")
_LOGGER.info("Stopped WebSocket notification listener")
def get_thing_class_name(self, thingclass_id: str) -> str | None:
"""Get the display name of a thing class by its ID."""
for thing_class in self.thing_classes:
if thing_class.get("id") == thingclass_id:
return thing_class.get("displayName")
return None
async def discover_and_log_all_things(self) -> None:
"""Discover and log all available thing classes and things from the Nymea system."""
try:
# Get all vendors
loop = self._hass.loop
vendors_response = await loop.run_in_executor(
None, self.send_command, "Integrations.GetVendors", None
)
vendors = vendors_response.get("params", {}).get("vendors", [])
# Create vendor lookup and store
vendor_map = {v["id"]: v for v in vendors}
self.vendors = vendor_map
# Get all thing classes
thing_classes_response = await loop.run_in_executor(
None, self.send_command, "Integrations.GetThingClasses", None
)
thing_classes = thing_classes_response.get("params", {}).get(
"thingClasses", []
)
# Store thing classes for dynamic entity generation
self.thing_classes = thing_classes
# Get all things
things_response = await loop.run_in_executor(
None, self.send_command, "Integrations.GetThings", None
)
things = things_response.get("params", {}).get("things", [])
# Build output as strings to reduce number of log calls
output = []
output.append("=" * 80)
output.append("NYMEA DISCOVERY STARTING")
output.append("=" * 80)
output.append(
f"Found {len(vendors)} vendors, {len(thing_classes)} thing classes, {len(things)} things (devices)"
)
output.append("-" * 80)
output.append("THING CLASSES AVAILABLE:")
output.append("-" * 80)
# Log each thing class with detailed info
for tc in thing_classes:
vendor = vendor_map.get(tc.get("vendorId"), {})
vendor_name = vendor.get("displayName", "Unknown")
output.append("")
output.append(f"Thing Class: {tc.get('displayName', 'N/A')}")
output.append(f" - ID: {tc.get('id', 'N/A')}")
output.append(f" - Vendor: {vendor_name}")
output.append(f" - Vendor ID: {tc.get('vendorId', 'N/A')}")
# Log available state types
state_types = tc.get("stateTypes", [])
if state_types:
output.append(f" - State Types ({len(state_types)}):")
for st in state_types:
output.append(
f" * {st.get('displayName', 'N/A')} (ID: {st.get('id', 'N/A')}, Type: {st.get('type', 'N/A')})"
)
# Log available action types
action_types = tc.get("actionTypes", [])
if action_types:
output.append(f" - Action Types ({len(action_types)}):")
for at in action_types:
output.append(
f" * {at.get('displayName', 'N/A')} (ID: {at.get('id', 'N/A')})"
)
# Log available event types
event_types = tc.get("eventTypes", [])
if event_types:
output.append(f" - Event Types ({len(event_types)}):")
for et in event_types:
output.append(
f" * {et.get('displayName', 'N/A')} (ID: {et.get('id', 'N/A')})"
)
output.append("")
output.append("-" * 80)
output.append("THINGS (DEVICES) CONFIGURED:")
output.append("-" * 80)
# Log each thing instance
for thing in things:
thing_class = next(
(
tc
for tc in thing_classes
if tc["id"] == thing.get("thingClassId")
),
None,
)
output.append("")
output.append(f"Device: {thing.get('name', 'N/A')}")
output.append(f" - Thing ID: {thing.get('id', 'N/A')}")
output.append(f" - Thing Class ID: {thing.get('thingClassId', 'N/A')}")
if thing_class:
vendor = vendor_map.get(thing_class.get("vendorId"), {})
output.append(
f" - Thing Class: {thing_class.get('displayName', 'N/A')}"
)
output.append(f" - Vendor: {vendor.get('displayName', 'Unknown')}")
# Get current states for this thing
states_response = await loop.run_in_executor(
None,
self.send_command,
"Integrations.GetStateValues",
{"thingId": thing.get("id")},
)
if states_response:
values = states_response.get("params", {}).get("values", [])
if values:
output.append(" - Current States:")
for state_value in values:
state_type_id = state_value.get("stateTypeId")
value = state_value.get("value")
# Try to find the state type name
state_type_name = "Unknown"
if thing_class:
state_types = thing_class.get("stateTypes", [])
state_type = next(
(
st
for st in state_types
if st["id"] == state_type_id
),
None,
)
if state_type:
state_type_name = state_type.get(
"displayName", "Unknown"
)
output.append(f" * {state_type_name}: {value}")
output.append("")
output.append("=" * 80)
output.append("NYMEA DISCOVERY COMPLETE")
output.append("=" * 80)
# Log everything as a single multi-line message
_LOGGER.info("Nymea Discovery Results:\n%s", "\n".join(output))
except Exception:
_LOGGER.exception("Error during Nymea discovery")