thepihub 0.1.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.
Files changed (111) hide show
  1. thepihub-0.1.0/LICENSE +202 -0
  2. thepihub-0.1.0/NOTICE +4 -0
  3. thepihub-0.1.0/PKG-INFO +288 -0
  4. thepihub-0.1.0/README.md +33 -0
  5. thepihub-0.1.0/pyproject.toml +47 -0
  6. thepihub-0.1.0/sdk/pihub/README.md +261 -0
  7. thepihub-0.1.0/sdk/pihub/__init__.py +42 -0
  8. thepihub-0.1.0/sdk/pihub/__main__.py +7 -0
  9. thepihub-0.1.0/sdk/pihub/board_leds.py +121 -0
  10. thepihub-0.1.0/sdk/pihub/camera_config.py +119 -0
  11. thepihub-0.1.0/sdk/pihub/camera_setup.py +163 -0
  12. thepihub-0.1.0/sdk/pihub/cli.py +353 -0
  13. thepihub-0.1.0/sdk/pihub/device.py +903 -0
  14. thepihub-0.1.0/sdk/pihub/device_alerts.py +123 -0
  15. thepihub-0.1.0/sdk/pihub/device_config.py +86 -0
  16. thepihub-0.1.0/sdk/pihub/device_identity.py +175 -0
  17. thepihub-0.1.0/sdk/pihub/device_information.py +393 -0
  18. thepihub-0.1.0/sdk/pihub/device_lock.py +157 -0
  19. thepihub-0.1.0/sdk/pihub/device_runtime_service.py +178 -0
  20. thepihub-0.1.0/sdk/pihub/events/__init__.py +3 -0
  21. thepihub-0.1.0/sdk/pihub/events/bus.py +90 -0
  22. thepihub-0.1.0/sdk/pihub/gpio.py +49 -0
  23. thepihub-0.1.0/sdk/pihub/lifecycle/__init__.py +0 -0
  24. thepihub-0.1.0/sdk/pihub/link_code_client.py +85 -0
  25. thepihub-0.1.0/sdk/pihub/notifications/__init__.py +0 -0
  26. thepihub-0.1.0/sdk/pihub/program.py +515 -0
  27. thepihub-0.1.0/sdk/pihub/properties/__init__.py +22 -0
  28. thepihub-0.1.0/sdk/pihub/properties/base.py +103 -0
  29. thepihub-0.1.0/sdk/pihub/properties/boolean.py +24 -0
  30. thepihub-0.1.0/sdk/pihub/properties/number.py +46 -0
  31. thepihub-0.1.0/sdk/pihub/properties/select.py +30 -0
  32. thepihub-0.1.0/sdk/pihub/properties/string.py +31 -0
  33. thepihub-0.1.0/sdk/pihub/protocol/__init__.py +0 -0
  34. thepihub-0.1.0/sdk/pihub/protocol/bindings.py +55 -0
  35. thepihub-0.1.0/sdk/pihub/protocol/envelope.py +91 -0
  36. thepihub-0.1.0/sdk/pihub/protocol/messages.py +492 -0
  37. thepihub-0.1.0/sdk/pihub/reconnect.py +76 -0
  38. thepihub-0.1.0/sdk/pihub/registration/__init__.py +8 -0
  39. thepihub-0.1.0/sdk/pihub/registration/manifest_builder.py +63 -0
  40. thepihub-0.1.0/sdk/pihub/registration_client.py +95 -0
  41. thepihub-0.1.0/sdk/pihub/runtime/__init__.py +9 -0
  42. thepihub-0.1.0/sdk/pihub/runtime/store.py +88 -0
  43. thepihub-0.1.0/sdk/pihub/runtime/value.py +51 -0
  44. thepihub-0.1.0/sdk/pihub/security.py +69 -0
  45. thepihub-0.1.0/sdk/pihub/service.py +261 -0
  46. thepihub-0.1.0/sdk/pihub/settings_store.py +73 -0
  47. thepihub-0.1.0/sdk/pihub/transport/__init__.py +25 -0
  48. thepihub-0.1.0/sdk/pihub/transport/console.py +81 -0
  49. thepihub-0.1.0/sdk/pihub/transport/websocket.py +314 -0
  50. thepihub-0.1.0/sdk/pihub/video/__init__.py +81 -0
  51. thepihub-0.1.0/sdk/pihub/video/best.py +69 -0
  52. thepihub-0.1.0/sdk/pihub/video/colour.py +130 -0
  53. thepihub-0.1.0/sdk/pihub/video/hardware.py +358 -0
  54. thepihub-0.1.0/sdk/pihub/video/recording.py +971 -0
  55. thepihub-0.1.0/sdk/pihub/video/software.py +66 -0
  56. thepihub-0.1.0/sdk/pihub/video/sources.py +736 -0
  57. thepihub-0.1.0/sdk/pihub/video/stills.py +71 -0
  58. thepihub-0.1.0/sdk/pihub/video/track.py +66 -0
  59. thepihub-0.1.0/sdk/pihub/webrtc/__init__.py +17 -0
  60. thepihub-0.1.0/sdk/pihub/webrtc/limits.py +32 -0
  61. thepihub-0.1.0/sdk/pihub/webrtc/peer_manager.py +1123 -0
  62. thepihub-0.1.0/sdk/pihub/widgets/__init__.py +47 -0
  63. thepihub-0.1.0/sdk/pihub/widgets/base.py +73 -0
  64. thepihub-0.1.0/sdk/pihub/widgets/camera.py +249 -0
  65. thepihub-0.1.0/sdk/pihub/widgets/command.py +48 -0
  66. thepihub-0.1.0/sdk/pihub/widgets/files.py +138 -0
  67. thepihub-0.1.0/sdk/pihub/widgets/output.py +75 -0
  68. thepihub-0.1.0/sdk/pihub/widgets/presentation.py +27 -0
  69. thepihub-0.1.0/sdk/pihub/widgets/registry.py +102 -0
  70. thepihub-0.1.0/sdk/pihub/widgets/setting.py +164 -0
  71. thepihub-0.1.0/sdk/thepihub.egg-info/PKG-INFO +288 -0
  72. thepihub-0.1.0/sdk/thepihub.egg-info/SOURCES.txt +109 -0
  73. thepihub-0.1.0/sdk/thepihub.egg-info/dependency_links.txt +1 -0
  74. thepihub-0.1.0/sdk/thepihub.egg-info/entry_points.txt +2 -0
  75. thepihub-0.1.0/sdk/thepihub.egg-info/requires.txt +12 -0
  76. thepihub-0.1.0/sdk/thepihub.egg-info/top_level.txt +1 -0
  77. thepihub-0.1.0/setup.cfg +4 -0
  78. thepihub-0.1.0/tests/test_best_camera_source.py +163 -0
  79. thepihub-0.1.0/tests/test_board_leds.py +109 -0
  80. thepihub-0.1.0/tests/test_camera_colour_watch.py +230 -0
  81. thepihub-0.1.0/tests/test_camera_component.py +219 -0
  82. thepihub-0.1.0/tests/test_camera_hooks.py +244 -0
  83. thepihub-0.1.0/tests/test_camera_orientation.py +278 -0
  84. thepihub-0.1.0/tests/test_camera_settings.py +623 -0
  85. thepihub-0.1.0/tests/test_camera_setup.py +382 -0
  86. thepihub-0.1.0/tests/test_camera_still_padding.py +133 -0
  87. thepihub-0.1.0/tests/test_cli.py +421 -0
  88. thepihub-0.1.0/tests/test_device.py +295 -0
  89. thepihub-0.1.0/tests/test_device_alerts.py +159 -0
  90. thepihub-0.1.0/tests/test_device_config.py +111 -0
  91. thepihub-0.1.0/tests/test_device_identity.py +240 -0
  92. thepihub-0.1.0/tests/test_device_information.py +629 -0
  93. thepihub-0.1.0/tests/test_device_lock.py +205 -0
  94. thepihub-0.1.0/tests/test_device_registration.py +170 -0
  95. thepihub-0.1.0/tests/test_device_runtime_service.py +268 -0
  96. thepihub-0.1.0/tests/test_device_simulation.py +251 -0
  97. thepihub-0.1.0/tests/test_files_widget.py +176 -0
  98. thepihub-0.1.0/tests/test_gpio.py +70 -0
  99. thepihub-0.1.0/tests/test_link_code_client.py +119 -0
  100. thepihub-0.1.0/tests/test_motion_frames.py +201 -0
  101. thepihub-0.1.0/tests/test_package_exports.py +36 -0
  102. thepihub-0.1.0/tests/test_packaging.py +37 -0
  103. thepihub-0.1.0/tests/test_pihub_install.py +259 -0
  104. thepihub-0.1.0/tests/test_program.py +483 -0
  105. thepihub-0.1.0/tests/test_reconnect.py +496 -0
  106. thepihub-0.1.0/tests/test_recording.py +627 -0
  107. thepihub-0.1.0/tests/test_register_if_needed.py +402 -0
  108. thepihub-0.1.0/tests/test_registration_client.py +184 -0
  109. thepihub-0.1.0/tests/test_sections.py +582 -0
  110. thepihub-0.1.0/tests/test_service.py +317 -0
  111. thepihub-0.1.0/tests/test_software_encoded_source.py +401 -0
thepihub-0.1.0/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
thepihub-0.1.0/NOTICE ADDED
@@ -0,0 +1,4 @@
1
+ ThePiHub SDK
2
+ Copyright 2026 Nick Harris
3
+
4
+ This product includes software developed by Nick Harris (https://thepihub.com).
@@ -0,0 +1,288 @@
1
+ Metadata-Version: 2.4
2
+ Name: thepihub
3
+ Version: 0.1.0
4
+ Summary: The ThePiHub device SDK: connect a Raspberry Pi to a web dashboard
5
+ Author: ThePiHub
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://thepihub.com
8
+ Keywords: raspberry-pi,iot,dashboard,gpio,webrtc
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: POSIX :: Linux
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Topic :: Home Automation
15
+ Classifier: Topic :: System :: Hardware
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ License-File: NOTICE
20
+ Requires-Dist: websocket-client>=1.9
21
+ Requires-Dist: aiortc>=1.9; platform_machine == "x86_64" or platform_machine == "AMD64" or platform_machine == "aarch64" or platform_machine == "arm64"
22
+ Provides-Extra: webrtc
23
+ Requires-Dist: aiortc>=1.9; (platform_machine == "x86_64" or platform_machine == "AMD64" or platform_machine == "aarch64" or platform_machine == "arm64") and extra == "webrtc"
24
+ Provides-Extra: gpio
25
+ Requires-Dist: gpiozero>=2.0; extra == "gpio"
26
+ Dynamic: license-file
27
+
28
+ # ThePiHub SDK
29
+
30
+ Put a Raspberry Pi (or any Linux machine) on a web dashboard without writing any web code. You say what the device has - a temperature to
31
+ show, a button that runs your code, a setting to change, a camera - and ThePiHub draws the dashboard, keeps it live, and works when you
32
+ are away from home. You never lay anything out.
33
+
34
+ ```python
35
+ from pihub import Device, Program
36
+
37
+ device = Device()
38
+
39
+ weather = Program(id="weather", name="Weather Station")
40
+ temperature = weather.output(id="temperature", type="Gauge")
41
+ device.add_program(weather)
42
+ device.start()
43
+
44
+ temperature.set(21.5) # the gauge on the dashboard now reads 21.5
45
+ ```
46
+
47
+ The first time you run it at a terminal, it prints a link and a code to approve in your browser, and the platform gives the Pi its id; every run after
48
+ that connects straight away. There is nothing to add to your program for that. (A program with no terminal, such as a service started at boot, never
49
+ waits for anyone: `pihub install my_program.py` links the machine and installs the service in one step.)
50
+
51
+ This package is the **device** side only. It talks to ThePiHub, the hosted dashboard at <https://thepihub.com>, where you need an account.
52
+ It is alpha software (0.1): expect the API to move.
53
+
54
+ ## Install
55
+
56
+ ```bash
57
+ pip install thepihub
58
+ ```
59
+
60
+ The package is called `thepihub`; you `import pihub`, and the command is `pihub`. Python 3.10 or newer, Linux (built for Raspberry Pi OS).
61
+
62
+ That one command installs everything pip can install. It includes the WebRTC library (aiortc and PyAV), so a browser can connect straight to your device and the
63
+ live **Camera** widget works. (`thepihub[webrtc]`, which older instructions use, still works and means the same.)
64
+
65
+ Two things come from somewhere else:
66
+
67
+ - **On a 32-bit Pi** (an original Pi Zero) pip has no ready-made PyAV, so the install leaves the WebRTC library out. Install it with apt instead, and make the virtualenv
68
+ with `--system-site-packages`: `sudo apt install python3-picamera2 python3-av python3-aiortc`. If it is missing, the device says so in its log when it starts.
69
+ - **The Pi camera and the pin driver** are Raspberry Pi OS packages, not something pip can install: `sudo apt install python3-picamera2` for the camera.
70
+
71
+ One optional extra: `pip install "thepihub[gpio]"` adds gpiozero, for buttons, LEDs and sensors on the Pi's pins (see GPIO pins below).
72
+
73
+ ## First run
74
+
75
+ The first time a machine connects, the program prints a link and a code. Open the link while logged in to ThePiHub, approve it, and
76
+ answer `y` in the terminal when it asks you to confirm the account. The device is then yours and the platform gives it its id; every
77
+ later run connects straight away. Nothing is typed onto the Pi but the `y`, so it works headless over SSH.
78
+
79
+ The saved identity lives in `~/.config/thepihub/config.json`. To set a machine up without running a program, use `pihub setup`.
80
+ The server is `https://thepihub.com` unless you pass `--server` or set `PIHUB_SERVER`.
81
+
82
+ ## How it fits together
83
+
84
+ ```text
85
+ Device this machine, connected to the platform
86
+ Program a named group of things; it becomes one section of the dashboard
87
+ output a value you publish -> Gauge, Status, Chart, Camera
88
+ command something the dashboard can run -> Button
89
+ setting a value the dashboard can change -> Toggle, Select, Slider
90
+ ```
91
+
92
+ The **device** says what it has. The **platform** owns the dashboards, the layout and how things look: you choose a widget by name and
93
+ give it a title; you never say where it goes. `group="Lights"` on any component is a hint that it belongs with others.
94
+
95
+ In the snippets below, `program` is a `Program` you create like the one at the top: declare its outputs, commands and settings, then
96
+ `device.add_program(program)` and `device.start()`, then publish values.
97
+
98
+ ### Outputs: show a value
99
+
100
+ ```python
101
+ from pihub import StringProperty
102
+
103
+ temperature = weather.output(id="temperature", type="Gauge",
104
+ properties=[StringProperty("title", default="Temperature"), StringProperty("units", default="°C")])
105
+ temperature.set(21.5) # one value
106
+ conditions = weather.output(id="conditions", type="Chart")
107
+ conditions.set_values({"temperature": 21.5, "humidity": 60}) # several at once, for a Chart
108
+ ```
109
+
110
+ While the connection is down `set()` does not raise: the latest value is sent when it comes back. Values are only sent to the platform
111
+ while someone is looking at the device (see Direct connections below).
112
+
113
+ ### Commands: run your code from the dashboard
114
+
115
+ ```python
116
+ lights = program.command(id="lights-on", type="Button", properties=[StringProperty("title", default="Lights on")])
117
+
118
+ @lights.handler
119
+ def pressed(command):
120
+ switch_the_relay_on()
121
+ program.info("Lights on") # a message in the dashboard's notification history
122
+ ```
123
+
124
+ ### Settings: a value the dashboard changes and the device remembers
125
+
126
+ ```python
127
+ program.setting("mode", "Select", default="Auto", options=["Auto", "Day", "Night"], title="Mode",
128
+ apply=lambda value: print("mode is now", value))
129
+ program.setting("brightness", "Slider", default=50, minimum=0, maximum=100, step=5, units="%")
130
+ program.setting("armed", "Toggle", default=False)
131
+ ```
132
+
133
+ The device checks a new value (one out of range is refused), saves it so it survives a restart, publishes it back and then calls `apply`.
134
+
135
+ ### Notifications
136
+
137
+ `program.info(...)`, `program.warning(...)` and `program.error(...)` appear on the dashboard. A warning stays until someone acknowledges it.
138
+
139
+ ### The Camera
140
+
141
+ ```python
142
+ from pihub.video import best_camera_source
143
+
144
+ camera = program.output(id="camera", type="Camera")
145
+ camera.use(best_camera_source()) # the Pi's camera (its hardware encoder, or software encoding where there is none); anywhere else, a moving test pattern
146
+ camera.recorder_if_possible() # adds a Record button when the camera can be recorded; recordings and snapshots are saved on the device
147
+ ```
148
+
149
+ `best_camera_source()` uses the Pi's hardware H.264 encoder where there is one (the Pi Zero to the Pi 4: cheap, about 5% of a Pi 3A+). Where there is none (a Pi 5) it encodes the Pi camera on the CPU with
150
+ libx264 (`SoftwareEncodedCameraSource`: about 40% of one core of a Pi 3A+ at 640x480, and limited to 640x480 until a Pi 5 has been measured). Both hand on H.264 packets, so both stream, take snapshots
151
+ and record (`camera.recorder_if_possible()`, and the Motion Trap). If PyAV has no libx264 it falls back to raw frames that `aiortc` encodes: streaming and snapshots only, no recording. On a computer with no camera it is a moving test pattern;
152
+ on a Raspberry Pi that cannot use its camera it raises `CameraUnavailable` saying what to check, rather than showing a test pattern as if it were the camera. `source.description` says which one it chose.
153
+
154
+ The picture goes straight from the device to the browser and only while someone is watching, so an idle camera costs nothing.
155
+
156
+ ## Examples
157
+
158
+ The source repository has small programs to copy, each a single file that connects to the real platform:
159
+
160
+ - `examples/widget_showcase.py` - every widget the dashboard can draw, in one program (the best place to start copying from)
161
+ - `examples/weather_station.py` - gauges and a chart
162
+ - `examples/greenhouse.py` - two gauges, a button that runs your code, and a warning
163
+ - `examples/birdbox.py` - a Pi camera on the dashboard
164
+
165
+ and, in `apps/`, complete programs to run as they are: a live bird-box camera, a GPIO panel (a button in, an LED out) and a motion-triggered camera trap.
166
+
167
+ ## Command Line
168
+
169
+ ```bash
170
+ pihub setup # link this machine with a code (headless onboarding)
171
+ pihub register TOKEN --device-id ID # exchange a registration token you already have
172
+ pihub camera setup # tell this machine what camera it has (a NoIR camera needs this)
173
+ pihub camera show
174
+ pihub install my_program.py # link this machine if needed, then run the program at boot (one step)
175
+ ```
176
+
177
+ `camera setup` looks at the camera's picture, asks whether it is a **NoIR** camera (no infrared filter) and saves the answer in
178
+ `camera.json` beside `config.json`. A NoIR camera is strongly pink until it is told, because libcamera picks its colour tuning by the
179
+ sensor's name and cannot tell the two apart. Every camera program then uses the answer by itself; `--noir` / `--normal` skip the
180
+ question, and `PiCameraSource(tuning=...)` / `HardwareCameraSource(tuning=...)` override it. Run it with the same `XDG_CONFIG_HOME`
181
+ as a service that uses its own. If nobody runs it, a program that calls `watch_camera_colour(source, device.information.warning)` (the sample app
182
+ does) raises one warning, from Device Information, the first time a real camera's picture is strongly pink; it points at the "Camera is NoIR" toggle there.
183
+
184
+ `setup` shows a link code, waits while the owner enters it in the web app,
185
+ asks for confirmation, then registers under a platform-issued `deviceId`.
186
+ Afterwards `Device()` (no arguments) loads that id from
187
+ `~/.config/thepihub/config.json`; `Device(id=...)` still works unchanged.
188
+ The server comes from `--server`, else `$PIHUB_SERVER`, else the default.
189
+
190
+ ## Grouping components (a hint)
191
+
192
+ Any component can say it belongs with others: `output(..., group="Camera")`, `command(...)` and `setting(...)` take the same argument. It is sent as an
193
+ ordinary `group` property and it is only a **hint**: the dashboard shows a section's ungrouped components first, then each group under a small
194
+ heading, and is free to ignore it. Device Information puts the camera's sensor and its settings (Resolution, Frame rate, Bitrate, Camera is NoIR)
195
+ in the group `"Camera"`.
196
+
197
+ ## Device Information (automatic)
198
+
199
+ A device that built its own connection (after `pihub setup`) also gets a **Device Information** program without any code of its own: it
200
+ is registered first, and shows the Pi model, hostname, OS, SDK version, uptime, CPU usage, temperature, memory, disk, power supply
201
+ (under-voltage or throttling), Wi-Fi signal, and the camera sensor if the device has a camera. Each field appears only if its source is
202
+ on that machine (a plain Linux server gets fewer). The live readings are taken only while someone is viewing the device. It is read-only
203
+ and does not include the serial number or machine id.
204
+
205
+ ```python
206
+ Device() # on (a real device)
207
+ Device(device_information=False) # off
208
+ Device(device_information=DeviceInformation(show_ip_address=True)) # on, plus the IP address (off by default)
209
+ ```
210
+
211
+ A device given its own transport (a simulation or a test) does not get it unless you ask.
212
+
213
+ ## Alerts from the machine's vitals
214
+
215
+ Device Information also **watches the vitals itself**, once a minute, whether or not anyone is viewing (about 0.01% of a Pi Zero W's one core), and
216
+ sends a notification from Device Information when something is wrong: a **warning** and an **error** level for CPU temperature (70 and 80 °C), CPU
217
+ usage (90 and 98%, over five checks in a row, so a brief spike is not an alert), memory (90 and 97%) and disk (85 and 95%). The dashboard asks the user
218
+ to acknowledge both.
219
+
220
+ It says so **once per episode**, not at every check: a reading that stays high says nothing more, and it must fall a margin below the level before it
221
+ counts as recovered (so a reading hovering around a level cannot flap); recovery is an information notice. The levels are provisional, a first guess.
222
+
223
+ ```python
224
+ DeviceInformation(alerts=False) # no alerts
225
+ DeviceInformation(alerts=[AlertRule(...)]) # your own levels (pihub.device_alerts)
226
+ DeviceInformation(alert_seconds=300) # check every 5 minutes instead
227
+ ```
228
+
229
+ Any section can run its own periodic work the same way: `section.every(seconds, func)` (before `start()`; first run one interval after start).
230
+
231
+ ## Direct connections (WebRTC)
232
+
233
+ A browser can connect straight to the device (the WebRTC library is part of the install; see Install). Then every runtime value the device publishes also goes to that browser over
234
+ its DataChannel (a viewer that has just connected is first sent the latest value of everything), and the browser may send commands the same way. A command from a
235
+ viewer reaches your handlers exactly like one from the platform (`@command.handler`), on its own thread, so a slow handler cannot stall the connection. The device only
236
+ accepts one while that viewer's session is alive, and limits it: a burst of 50, then 30 a second per viewer; a viewer is told at once whether it was handed over (202) or
237
+ refused, but not whether it was done.
238
+
239
+ **The platform only gets a value while it is needed.** With nobody watching, or every viewer connected directly, runtime values are not sent to the platform at all (notifications, registration and
240
+ signalling still are), which is what keeps a device's cost down. A viewer that is still connecting, or has failed, is served through the platform, and the moment that starts the device sends it a
241
+ snapshot of the latest of everything. So the platform's stored copy of a value is only as fresh as the last time it was needed: `GET /devices/{id}/runtime` says "the last value the platform received".
242
+ A device without the extra sends everything to the platform, as it always did. The device and the page each say what they support when the channel opens, and each relies on the direct path only once
243
+ the other has said it can, so an older page (still open in a tab) or an older device is left on the platform's path.
244
+
245
+ ## Staying Connected
246
+
247
+ A device that built its own connection (the normal case, after `pihub setup`)
248
+ reconnects by itself when the connection drops - a server redeploy, a wifi
249
+ blip, a pulled cable. It retries forever with a growing delay (1s to 60s,
250
+ jittered), and on reconnect re-announces itself and re-sends the latest value
251
+ of each output, so the dashboard is never left empty.
252
+
253
+ ```python
254
+ Device() # reconnects (its own connection)
255
+ Device(id="x", transport=t) # does not (you supplied the transport)
256
+ Device(reconnect=False) # switched off explicitly
257
+ ```
258
+
259
+ While reconnecting, `Output.set()` does not raise; the latest value is sent
260
+ once the connection is back. A refused login (HTTP 403) is retried with a
261
+ clear error in the log and never given up on - the platform gives the same
262
+ answer for a bad secret and for a rate-limited device.
263
+
264
+ ## GPIO pins (sensors, LEDs, buttons)
265
+
266
+ Apps drive pins with gpiozero (`pip install "thepihub[gpio]"`). Call `pihub.gpio.prepare_pins()` before making the first gpiozero
267
+ device: gpiozero's driver on current Raspberry Pi OS, `lgpio`, comes from apt (`python3-lgpio`) and does not build under pip, so a
268
+ venv without system packages would otherwise fall back to gpiozero's own driver, which cannot watch a pin for changes on current
269
+ kernels (a `Button` or `DigitalInputDevice` fails with `OSError: [Errno 22]`). `prepare_pins()` lets the venv see the system's
270
+ `lgpio`, as the camera code already does for picamera2.
271
+
272
+ ## Running At Boot (opt-in)
273
+
274
+ Nothing is installed unless you ask:
275
+
276
+ ```bash
277
+ pihub service install apps/birdbox_live.py [--name N] [--config-home DIR]
278
+ pihub service status [N]
279
+ pihub service uninstall N
280
+ ```
281
+
282
+ It creates a systemd *user* service that restarts the program if it dies.
283
+ To start at boot with nobody logged in, run once:
284
+ `sudo loginctl enable-linger <user>` (the command prints this when needed).
285
+
286
+ ## Licence
287
+
288
+ Apache License 2.0; see `LICENSE` and `NOTICE`.
@@ -0,0 +1,33 @@
1
+ # ThePiHub SDK
2
+
3
+ The Python SDK for [ThePiHub](https://thepihub.com): connect a Raspberry Pi (or any Linux machine) to a web dashboard.
4
+
5
+ ```bash
6
+ pip install thepihub
7
+ ```
8
+
9
+ The package is called `thepihub`, you `import pihub`, and the command is `pihub`. **Start with [the SDK guide](sdk/pihub/README.md)**: what it does,
10
+ how to install it, your first run and the concepts, with code.
11
+
12
+ | Folder | What is in it |
13
+ |---|---|
14
+ | [`sdk/pihub/`](sdk/pihub/) | the SDK itself |
15
+ | [`examples/`](examples/) | small programs to copy, each connecting to the real platform; start with `widget_showcase.py`, which shows every widget |
16
+ | [`apps/`](apps/) | complete programs to run as they are: a live bird-box camera, a GPIO panel, a motion-triggered camera trap |
17
+ | [`tests/`](tests/) | the SDK's tests |
18
+
19
+ ## Working on the SDK
20
+
21
+ ```bash
22
+ python -m venv .venv && source .venv/bin/activate
23
+ pip install -e ".[gpio]" pytest
24
+ python -m pytest tests -q
25
+ ```
26
+
27
+ Use `python -m pytest`, not bare `pytest`: the tests import the programs in `examples/`.
28
+
29
+ This repository is a copy of the SDK published from ThePiHub's main repository.
30
+
31
+ ## Licence
32
+
33
+ Apache License 2.0; see [`LICENSE`](LICENSE) and [`NOTICE`](NOTICE).
@@ -0,0 +1,47 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "thepihub"
7
+ version = "0.1.0"
8
+ description = "The ThePiHub device SDK: connect a Raspberry Pi to a web dashboard"
9
+ authors = [{ name = "ThePiHub" }]
10
+ readme = { file = "sdk/pihub/README.md", content-type = "text/markdown" }
11
+ keywords = ["raspberry-pi", "iot", "dashboard", "gpio", "webrtc"]
12
+ classifiers = [
13
+ "Development Status :: 3 - Alpha",
14
+ "Intended Audience :: Developers",
15
+ "Operating System :: POSIX :: Linux",
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: Python :: 3 :: Only",
18
+ "Topic :: Home Automation",
19
+ "Topic :: System :: Hardware",
20
+ ]
21
+ license = "Apache-2.0"
22
+ license-files = ["LICENSE", "NOTICE"]
23
+ requires-python = ">=3.10"
24
+ dependencies = [
25
+ "websocket-client>=1.9",
26
+ # Direct browser-to-device connections, live video and the Camera widget (aiortc brings PyAV). Part of the plain install, so nobody has to
27
+ # know to ask for it. Only where PyAV has pip wheels (the allow-list below): on a 32-bit Raspberry Pi pip cannot build it, so the dependency is
28
+ # skipped there and the apt packages are the route (python3-aiortc python3-av, in a virtualenv made with --system-site-packages). The device
29
+ # says so at run time if the library is missing.
30
+ 'aiortc>=1.9; platform_machine == "x86_64" or platform_machine == "AMD64" or platform_machine == "aarch64" or platform_machine == "arm64"',
31
+ ]
32
+
33
+ [project.optional-dependencies]
34
+ # Kept so `pip install "thepihub[webrtc]"` (in older docs, hints and posts) still works: it is now the same as the plain install.
35
+ webrtc = ['aiortc>=1.9; platform_machine == "x86_64" or platform_machine == "AMD64" or platform_machine == "aarch64" or platform_machine == "arm64"']
36
+ # Sensors, LEDs and buttons on the GPIO pins (apps/motion_trap.py). Raspberry Pi OS already has it for its own Python;
37
+ # its mock pins let the apps run, and be tested, on any machine.
38
+ gpio = ["gpiozero>=2.0"]
39
+
40
+ [project.urls]
41
+ Homepage = "https://thepihub.com"
42
+
43
+ [project.scripts]
44
+ pihub = "pihub.cli:main"
45
+
46
+ [tool.setuptools.packages.find]
47
+ where = ["sdk"]