harbor-python 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- harbor_python-1.0.0/.gitignore +13 -0
- harbor_python-1.0.0/LICENSE +201 -0
- harbor_python-1.0.0/PKG-INFO +21 -0
- harbor_python-1.0.0/README.md +3 -0
- harbor_python-1.0.0/harbor/__init__.py +62 -0
- harbor_python-1.0.0/harbor/config.py +11 -0
- harbor_python-1.0.0/harbor/core.py +63 -0
- harbor_python-1.0.0/harbor/data/__init__.py +0 -0
- harbor_python-1.0.0/harbor/data/mqtt_models.py +123 -0
- harbor_python-1.0.0/harbor/device.py +179 -0
- harbor_python-1.0.0/harbor/devices/camera.py +177 -0
- harbor_python-1.0.0/harbor/devices/monitor.py +16 -0
- harbor_python-1.0.0/harbor/events.py +667 -0
- harbor_python-1.0.0/harbor/mqtt.py +201 -0
- harbor_python-1.0.0/harbor/py.typed +0 -0
- harbor_python-1.0.0/harbor/state.py +47 -0
- harbor_python-1.0.0/harbor/utils.py +39 -0
- harbor_python-1.0.0/pyproject.toml +78 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Harbor Systems
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: harbor-python
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A package to locally connect to a Harbor Sleep Camera
|
|
5
|
+
Author: Andres Garcia, Lash-L
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: baby,baby monitor,harbor
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Natural Language :: English
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
14
|
+
Requires-Python: <4,>=3.11
|
|
15
|
+
Requires-Dist: aiomqtt>=2.5.0
|
|
16
|
+
Requires-Dist: pydantic>=2.12.2
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# Harbor Python
|
|
20
|
+
|
|
21
|
+
A Python package to locally connect to Harbor Sleep Cameras.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from .config import HarborCameraConfig
|
|
2
|
+
from .core import Harbor
|
|
3
|
+
from .data.mqtt_models import (
|
|
4
|
+
HeartbeatEvent,
|
|
5
|
+
LocalLivekitHeartbeatEvent,
|
|
6
|
+
MotionDetectedEvent,
|
|
7
|
+
SettingsEvent,
|
|
8
|
+
ViewerJoinedEvent,
|
|
9
|
+
ViewerLeftEvent,
|
|
10
|
+
)
|
|
11
|
+
from .device import HarborDevice
|
|
12
|
+
from .devices.camera import HarborCamera
|
|
13
|
+
from .devices.monitor import HarborMonitor
|
|
14
|
+
from .events import (
|
|
15
|
+
CameraEventUpdate,
|
|
16
|
+
EventType,
|
|
17
|
+
HarborEvent,
|
|
18
|
+
HarborEventBus,
|
|
19
|
+
HeartbeatUpdate,
|
|
20
|
+
LocalLivekitHeartbeatUpdate,
|
|
21
|
+
MotionDetectedUpdate,
|
|
22
|
+
RawEventUpdate,
|
|
23
|
+
SettingsUpdate,
|
|
24
|
+
ViewerInfo,
|
|
25
|
+
ViewerJoinedUpdate,
|
|
26
|
+
ViewerLeftUpdate,
|
|
27
|
+
parse_message,
|
|
28
|
+
)
|
|
29
|
+
from .mqtt import HarborMQTTClient
|
|
30
|
+
from .state import HarborDeviceState, HarborEventState, HarborSourceType, HarborViewer
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"Harbor",
|
|
34
|
+
"HarborCameraConfig",
|
|
35
|
+
"HarborMQTTClient",
|
|
36
|
+
"HarborDevice",
|
|
37
|
+
"HarborCamera",
|
|
38
|
+
"HarborMonitor",
|
|
39
|
+
"HarborEvent",
|
|
40
|
+
"HarborEventBus",
|
|
41
|
+
"EventType",
|
|
42
|
+
"RawEventUpdate",
|
|
43
|
+
"HeartbeatUpdate",
|
|
44
|
+
"LocalLivekitHeartbeatUpdate",
|
|
45
|
+
"ViewerJoinedUpdate",
|
|
46
|
+
"ViewerLeftUpdate",
|
|
47
|
+
"SettingsUpdate",
|
|
48
|
+
"CameraEventUpdate",
|
|
49
|
+
"MotionDetectedUpdate",
|
|
50
|
+
"ViewerInfo",
|
|
51
|
+
"parse_message",
|
|
52
|
+
"HeartbeatEvent",
|
|
53
|
+
"LocalLivekitHeartbeatEvent",
|
|
54
|
+
"SettingsEvent",
|
|
55
|
+
"ViewerJoinedEvent",
|
|
56
|
+
"ViewerLeftEvent",
|
|
57
|
+
"MotionDetectedEvent",
|
|
58
|
+
"HarborSourceType",
|
|
59
|
+
"HarborViewer",
|
|
60
|
+
"HarborEventState",
|
|
61
|
+
"HarborDeviceState",
|
|
62
|
+
]
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from .config import HarborCameraConfig
|
|
5
|
+
from .device import HarborDevice
|
|
6
|
+
from .mqtt import HarborMQTTClient
|
|
7
|
+
|
|
8
|
+
_LOGGER = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Harbor:
|
|
12
|
+
"""
|
|
13
|
+
High-level API for managing Harbor devices and MQTT connections.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self) -> None:
|
|
17
|
+
self._devices: dict[str, HarborDevice] = {}
|
|
18
|
+
self._clients: dict[str, HarborMQTTClient] = {}
|
|
19
|
+
self._topics_cache: set[str] = set()
|
|
20
|
+
|
|
21
|
+
def add_device(self, device: HarborDevice) -> None:
|
|
22
|
+
"""Add a device (camera or monitor) to be managed."""
|
|
23
|
+
self._devices[device.serial] = device
|
|
24
|
+
self._topics_cache.update(device.get_topics())
|
|
25
|
+
_LOGGER.debug("Added device: %s (%s)", device.serial, type(device).__name__)
|
|
26
|
+
|
|
27
|
+
def add_camera_connection(self, config: HarborCameraConfig) -> None:
|
|
28
|
+
"""
|
|
29
|
+
Add a camera connection configuration.
|
|
30
|
+
This creates an MQTT client for the specified camera.
|
|
31
|
+
"""
|
|
32
|
+
if config.serial in self._clients:
|
|
33
|
+
_LOGGER.warning("Camera connection already exists: %s", config.serial)
|
|
34
|
+
return
|
|
35
|
+
topics = list(self._topics_cache)
|
|
36
|
+
client = HarborMQTTClient(
|
|
37
|
+
config=config,
|
|
38
|
+
topics=topics,
|
|
39
|
+
message_handler=self.handle_message,
|
|
40
|
+
client_id=f"harbor-client-{config.serial}",
|
|
41
|
+
)
|
|
42
|
+
self._clients[config.serial] = client
|
|
43
|
+
_LOGGER.info("Added MQTT client for camera: %s", config.serial)
|
|
44
|
+
|
|
45
|
+
async def start(self) -> None:
|
|
46
|
+
"""Start all MQTT clients."""
|
|
47
|
+
for client in self._clients.values():
|
|
48
|
+
await client.start()
|
|
49
|
+
|
|
50
|
+
async def stop(self) -> None:
|
|
51
|
+
"""Stop all MQTT clients and release device resources."""
|
|
52
|
+
for client in self._clients.values():
|
|
53
|
+
await client.stop()
|
|
54
|
+
for device in self._devices.values():
|
|
55
|
+
device.shutdown()
|
|
56
|
+
|
|
57
|
+
async def handle_message(self, topic: str, payload: Any) -> None:
|
|
58
|
+
"""
|
|
59
|
+
Central message handler.
|
|
60
|
+
Dispatches messages to all interested devices.
|
|
61
|
+
"""
|
|
62
|
+
for device in self._devices.values():
|
|
63
|
+
await device.handle_message(topic, payload)
|
|
File without changes
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class HarborMQTTPayload(BaseModel):
|
|
9
|
+
"""Base model for Harbor MQTT payloads."""
|
|
10
|
+
|
|
11
|
+
model_config = ConfigDict(extra="allow")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class LocalLivekitHeartbeatEvent(HarborMQTTPayload):
|
|
15
|
+
"""Payload for a local LiveKit heartbeat."""
|
|
16
|
+
|
|
17
|
+
app_start_time: str | None = None
|
|
18
|
+
app_version: str | None = None
|
|
19
|
+
bitrate: float | None = None
|
|
20
|
+
camera_present: bool | None = None
|
|
21
|
+
camera_state: str | None = None
|
|
22
|
+
is_healthy: dict[str, Any] = Field(default_factory=dict)
|
|
23
|
+
network_bars: int | None = None
|
|
24
|
+
os_version: str | None = None
|
|
25
|
+
receiver_present: bool | None = None
|
|
26
|
+
speaker_state: str | None = None
|
|
27
|
+
stream_quality: str | None = None
|
|
28
|
+
stream_start_time: str | None = None
|
|
29
|
+
viewers_by_identity: dict[str, Any] = Field(default_factory=dict)
|
|
30
|
+
viewers_by_identity_full: dict[str, Any] = Field(default_factory=dict)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class HeartbeatEvent(HarborMQTTPayload):
|
|
34
|
+
"""Payload for a device heartbeat."""
|
|
35
|
+
|
|
36
|
+
app_version: str | None = None
|
|
37
|
+
efuse_voltage: int | None = None
|
|
38
|
+
image_sensor_temperature: float | None = None
|
|
39
|
+
ntc_adc_voltage: int | None = None
|
|
40
|
+
ntc_temperature: float | None = None
|
|
41
|
+
os_version: str | None = None
|
|
42
|
+
raw_temperature: float | None = None
|
|
43
|
+
sensor_temperature: float | None = None
|
|
44
|
+
temperature: float | None = None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Settings(HarborMQTTPayload):
|
|
48
|
+
"""Camera settings included with a settings event."""
|
|
49
|
+
|
|
50
|
+
log_level: str | None = None
|
|
51
|
+
preference_ai: dict[str, Any] = Field(default_factory=dict)
|
|
52
|
+
preference_anomaly_configs: dict[str, Any] = Field(default_factory=dict)
|
|
53
|
+
preference_anomaly_throttle_duration_seconds: int | None = None
|
|
54
|
+
preference_auto_pinning: bool | None = None
|
|
55
|
+
preference_connection_band: str | None = None
|
|
56
|
+
preference_connection_bssid: str | None = None
|
|
57
|
+
preference_display_name: str | None = None
|
|
58
|
+
preference_moment_length: int | None = None
|
|
59
|
+
preference_operating_mode: str | None = None
|
|
60
|
+
preference_scheduled_reboot: str | None = None
|
|
61
|
+
preference_silence_alerting_until: str | None = None
|
|
62
|
+
preference_stream_paused: bool | None = None
|
|
63
|
+
preference_video_clock_display_tz_abbrev: str | None = None
|
|
64
|
+
preference_video_clock_display_tz_offset: int | None = None
|
|
65
|
+
preference_video_flip: bool | None = None
|
|
66
|
+
preference_video_has_clock_display: bool | None = None
|
|
67
|
+
preference_video_ir_brightness: int | None = None
|
|
68
|
+
preference_video_night_mode: str | None = None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class SettingsState(HarborMQTTPayload):
|
|
72
|
+
"""Runtime state attached to a settings event."""
|
|
73
|
+
|
|
74
|
+
application_state: int | None = None
|
|
75
|
+
network_bars: int | None = None
|
|
76
|
+
stream_state: int | None = None
|
|
77
|
+
temperature: float | None = None
|
|
78
|
+
video_night_mode: bool | None = None
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class SettingsEvent(HarborMQTTPayload):
|
|
82
|
+
"""Payload for a settings event."""
|
|
83
|
+
|
|
84
|
+
client: str | None = None
|
|
85
|
+
is_updating: bool | None = None
|
|
86
|
+
seq: str | None = None
|
|
87
|
+
settings: Settings | None = None
|
|
88
|
+
state: SettingsState | None = None
|
|
89
|
+
triggered_by: str | None = None
|
|
90
|
+
updated: dict[str, Any] = Field(default_factory=dict)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class ViewerJoinedEvent(HarborMQTTPayload):
|
|
94
|
+
"""Payload for a viewer joined event."""
|
|
95
|
+
|
|
96
|
+
client: str | None = None
|
|
97
|
+
identity: str | None = None
|
|
98
|
+
is_local: bool | None = None
|
|
99
|
+
role: str | None = None
|
|
100
|
+
viewer_id: str | None = None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class ViewerLeftEvent(HarborMQTTPayload):
|
|
104
|
+
"""Payload for a viewer left event."""
|
|
105
|
+
|
|
106
|
+
client: str | None = None
|
|
107
|
+
identity: str | None = None
|
|
108
|
+
is_local: bool | None = None
|
|
109
|
+
role: str | None = None
|
|
110
|
+
viewer_id: str | None = None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class MotionDetectedEvent(HarborMQTTPayload):
|
|
114
|
+
"""Payload for a motion detection event."""
|
|
115
|
+
|
|
116
|
+
active_config: str | None = None
|
|
117
|
+
duration: str | float | int | None = None
|
|
118
|
+
filename: str | None = None
|
|
119
|
+
level: str | None = None
|
|
120
|
+
sensitivity: str | None = None
|
|
121
|
+
threshold: str | None = None
|
|
122
|
+
thumbnail: str | None = None
|
|
123
|
+
timestamp: str | None = None
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import logging
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from datetime import UTC
|
|
8
|
+
from typing import Any, TypeVar, overload
|
|
9
|
+
|
|
10
|
+
from pydantic import BaseModel
|
|
11
|
+
|
|
12
|
+
from .data.mqtt_models import (
|
|
13
|
+
HeartbeatEvent as HarborHeartbeatPayload,
|
|
14
|
+
)
|
|
15
|
+
from .data.mqtt_models import (
|
|
16
|
+
SettingsEvent as HarborSettingsPayload,
|
|
17
|
+
)
|
|
18
|
+
from .events import (
|
|
19
|
+
HarborEvent,
|
|
20
|
+
HeartbeatUpdate,
|
|
21
|
+
SettingsUpdate,
|
|
22
|
+
SubscriptionManager,
|
|
23
|
+
parse_message,
|
|
24
|
+
)
|
|
25
|
+
from .state import HarborDeviceState, HarborSourceType
|
|
26
|
+
|
|
27
|
+
LOGGER = logging.getLogger(__name__)
|
|
28
|
+
|
|
29
|
+
UpdateCallbackType = Callable[[HarborDeviceState], Any]
|
|
30
|
+
EventT = TypeVar("EventT", bound=HarborEvent)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class HarborDevice(ABC):
|
|
34
|
+
"""Abstract base class for Harbor devices."""
|
|
35
|
+
|
|
36
|
+
def __init__(self, serial: str, source_type: HarborSourceType) -> None:
|
|
37
|
+
self.serial = serial
|
|
38
|
+
self._source_type = source_type
|
|
39
|
+
self.state = HarborDeviceState(serial=serial, source_type=source_type)
|
|
40
|
+
self._subscriptions = SubscriptionManager()
|
|
41
|
+
self._update_subscriptions: list[UpdateCallbackType] = []
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def source_type(self) -> HarborSourceType:
|
|
45
|
+
"""Return the source type for events from this device."""
|
|
46
|
+
return self._source_type
|
|
47
|
+
|
|
48
|
+
@abstractmethod
|
|
49
|
+
def get_topics(self) -> list[str]:
|
|
50
|
+
"""Return a list of topics to subscribe to."""
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
async def handle_message(self, topic: str, payload: Any) -> HarborEvent | None:
|
|
54
|
+
"""Handle an incoming MQTT message."""
|
|
55
|
+
if not (event := parse_message(topic, payload)):
|
|
56
|
+
return None
|
|
57
|
+
|
|
58
|
+
if event.source_type != self.source_type or event.source_sn != self.serial:
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
self._apply_event(event)
|
|
62
|
+
await self._emit_event(event)
|
|
63
|
+
await self._emit_update()
|
|
64
|
+
return event
|
|
65
|
+
|
|
66
|
+
@overload
|
|
67
|
+
def subscribe(
|
|
68
|
+
self,
|
|
69
|
+
event_type: type[EventT],
|
|
70
|
+
callback: Callable[[EventT], Any],
|
|
71
|
+
) -> Callable[[], None]: ...
|
|
72
|
+
|
|
73
|
+
@overload
|
|
74
|
+
def subscribe(
|
|
75
|
+
self,
|
|
76
|
+
event_type: type[BaseModel],
|
|
77
|
+
callback: Callable[[HarborEvent], Any],
|
|
78
|
+
) -> Callable[[], None]: ...
|
|
79
|
+
|
|
80
|
+
@overload
|
|
81
|
+
def subscribe(
|
|
82
|
+
self,
|
|
83
|
+
event_type: None,
|
|
84
|
+
callback: Callable[[HarborEvent], Any],
|
|
85
|
+
) -> Callable[[], None]: ...
|
|
86
|
+
|
|
87
|
+
def subscribe(
|
|
88
|
+
self,
|
|
89
|
+
event_type: type[HarborEvent] | type[BaseModel] | None,
|
|
90
|
+
callback: Callable[[Any], Any],
|
|
91
|
+
) -> Callable[[], None]:
|
|
92
|
+
"""Subscribe to a specific Harbor event type or all events."""
|
|
93
|
+
|
|
94
|
+
return self._subscriptions.subscribe(event_type, callback)
|
|
95
|
+
|
|
96
|
+
async def _emit_event(self, event: HarborEvent) -> None:
|
|
97
|
+
"""Emit a parsed Harbor event to subscribers."""
|
|
98
|
+
|
|
99
|
+
await self._subscriptions.emit(event)
|
|
100
|
+
|
|
101
|
+
def subscribe_updates(
|
|
102
|
+
self,
|
|
103
|
+
callback: UpdateCallbackType,
|
|
104
|
+
) -> Callable[[], None]:
|
|
105
|
+
"""Subscribe to device state updates."""
|
|
106
|
+
self._update_subscriptions.append(callback)
|
|
107
|
+
|
|
108
|
+
def unsubscribe() -> None:
|
|
109
|
+
if callback in self._update_subscriptions:
|
|
110
|
+
self._update_subscriptions.remove(callback)
|
|
111
|
+
|
|
112
|
+
return unsubscribe
|
|
113
|
+
|
|
114
|
+
async def _emit_update(self) -> None:
|
|
115
|
+
"""Emit a state update to subscribers."""
|
|
116
|
+
for callback in list(self._update_subscriptions):
|
|
117
|
+
try:
|
|
118
|
+
result = callback(self.state)
|
|
119
|
+
if asyncio.iscoroutine(result):
|
|
120
|
+
await result
|
|
121
|
+
except Exception:
|
|
122
|
+
LOGGER.exception(
|
|
123
|
+
"Error in device update callback %s for %s",
|
|
124
|
+
getattr(callback, "__qualname__", repr(callback)),
|
|
125
|
+
self.serial,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
def shutdown(self) -> None:
|
|
129
|
+
"""Release device resources."""
|
|
130
|
+
|
|
131
|
+
def _apply_event(self, event: HarborEvent) -> None:
|
|
132
|
+
"""Apply a parsed Harbor event to device state.
|
|
133
|
+
|
|
134
|
+
Subclasses should override this to handle device-specific event types,
|
|
135
|
+
calling ``super()._apply_event(event)`` to keep the shared handling.
|
|
136
|
+
"""
|
|
137
|
+
self.state.last_seen = event.timestamp.astimezone(UTC)
|
|
138
|
+
self._update_shared_metadata(event)
|
|
139
|
+
|
|
140
|
+
match event:
|
|
141
|
+
case HeartbeatUpdate(payload=payload):
|
|
142
|
+
self._apply_heartbeat(payload)
|
|
143
|
+
case SettingsUpdate(payload=payload):
|
|
144
|
+
self._apply_settings(payload)
|
|
145
|
+
|
|
146
|
+
def _update_shared_metadata(self, event: HarborEvent) -> None:
|
|
147
|
+
"""Update metadata shared across Harbor event types."""
|
|
148
|
+
if event.os_version is not None:
|
|
149
|
+
self.state.os_version = event.os_version
|
|
150
|
+
|
|
151
|
+
if event.app_version is not None:
|
|
152
|
+
self.state.app_version = event.app_version
|
|
153
|
+
|
|
154
|
+
if event.display_name is not None:
|
|
155
|
+
self.state.display_name = event.display_name
|
|
156
|
+
|
|
157
|
+
def _apply_heartbeat(self, payload: HarborHeartbeatPayload) -> None:
|
|
158
|
+
"""Apply a device heartbeat payload."""
|
|
159
|
+
self._set_state_value("temperature", payload.temperature)
|
|
160
|
+
self._set_state_value("sensor_temperature", payload.sensor_temperature)
|
|
161
|
+
self._set_state_value("raw_temperature", payload.raw_temperature)
|
|
162
|
+
self._set_state_value(
|
|
163
|
+
"image_sensor_temperature",
|
|
164
|
+
payload.image_sensor_temperature,
|
|
165
|
+
)
|
|
166
|
+
self._set_state_value("ntc_temperature", payload.ntc_temperature)
|
|
167
|
+
|
|
168
|
+
def _apply_settings(self, payload: HarborSettingsPayload) -> None:
|
|
169
|
+
"""Apply a settings payload."""
|
|
170
|
+
if payload.state is None:
|
|
171
|
+
return
|
|
172
|
+
|
|
173
|
+
self._set_state_value("wifi_strength", payload.state.network_bars)
|
|
174
|
+
self._set_state_value("temperature", payload.state.temperature)
|
|
175
|
+
|
|
176
|
+
def _set_state_value(self, key: str, value: Any) -> None:
|
|
177
|
+
"""Set a state value when the payload exposed a concrete value."""
|
|
178
|
+
if value is not None:
|
|
179
|
+
self.state.values[key] = value
|