HAP-python 1.0.5__tar.gz → 1.0.7__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.
Potentially problematic release.
This version of HAP-python might be problematic. Click here for more details.
- {usr/local/lib/python3.6/site-packages/HAP_python-1.0.5-py3.6.egg-info → HAP-python-1.0.7/HAP_python.egg-info}/PKG-INFO +1 -1
- {usr/local/lib/python3.6/site-packages/HAP_python-1.0.5-py3.6.egg-info → HAP-python-1.0.7/HAP_python.egg-info}/SOURCES.txt +1 -0
- {usr/local/lib/python3.6/site-packages/HAP_python-1.0.5-py3.6.egg-info → HAP-python-1.0.7/HAP_python.egg-info}/requires.txt +4 -0
- HAP-python-1.0.7/LICENSE +176 -0
- HAP-python-1.0.7/MANIFEST.in +2 -0
- HAP-python-1.0.7/PKG-INFO +18 -0
- HAP-python-1.0.7/README.md +230 -0
- HAP-python-1.0.7/pyhap/accessories/SDS011.py +152 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessory.py +16 -1
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessory_driver.py +16 -1
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/characteristic.py +72 -43
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/hap_server.py +0 -0
- HAP-python-1.0.7/setup.cfg +10 -0
- HAP-python-1.0.7/setup.py +39 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/__init__.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/accessory.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/accessory_driver.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/characteristic.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/hap_server.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/hsrp.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/loader.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/params.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/service.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/tlv.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/__pycache__/util.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/AM2302.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/BMP180.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/FakeFan.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/Http.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/LightBulb.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/MotionSensor.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/ShutdownSwitch.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/TSL2591.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/TemperatureSensor.cpython-36.pyc +0 -0
- usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/__init__.cpython-36.pyc +0 -0
- {usr/local/lib/python3.6/site-packages/HAP_python-1.0.5-py3.6.egg-info → HAP-python-1.0.7/HAP_python.egg-info}/dependency_links.txt +0 -0
- {usr/local/lib/python3.6/site-packages/HAP_python-1.0.5-py3.6.egg-info → HAP-python-1.0.7/HAP_python.egg-info}/top_level.txt +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/__init__.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/AM2302.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/BMP180.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/FakeFan.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/Http.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/LightBulb.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/MotionSensor.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/ShutdownSwitch.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/TSL2591.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/TemperatureSensor.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/__init__.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/hsrp.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/loader.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/params.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/resources/characteristics.json +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/resources/services.json +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/service.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/tlv.py +0 -0
- {usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/util.py +0 -0
|
@@ -25,6 +25,7 @@ pyhap/accessories/FakeFan.py
|
|
|
25
25
|
pyhap/accessories/Http.py
|
|
26
26
|
pyhap/accessories/LightBulb.py
|
|
27
27
|
pyhap/accessories/MotionSensor.py
|
|
28
|
+
pyhap/accessories/SDS011.py
|
|
28
29
|
pyhap/accessories/ShutdownSwitch.py
|
|
29
30
|
pyhap/accessories/TSL2591.py
|
|
30
31
|
pyhap/accessories/TemperatureSensor.py
|
HAP-python-1.0.7/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
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
|
|
95
|
+
Derivative 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
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 1.1
|
|
2
|
+
Name: HAP-python
|
|
3
|
+
Version: 1.0.7
|
|
4
|
+
Summary: HomeKit Accessory Protocol implementation in python3
|
|
5
|
+
Home-page: https://github.com/ikalchev/HAP-python.git
|
|
6
|
+
Author: Ivan Kalchev
|
|
7
|
+
Author-email: UNKNOWN
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Description-Content-Type: UNKNOWN
|
|
10
|
+
Description: UNKNOWN
|
|
11
|
+
Platform: UNKNOWN
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Topic :: Home Automation
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
[](https://badge.fury.io/py/HAP-python)
|
|
2
|
+
# HAP-python
|
|
3
|
+
|
|
4
|
+
HomeKit Accessory Protocol implementation in python 3 (tested with 3.4, 3.5 and 3.6).
|
|
5
|
+
With this project, you can integrate your own accessories and add them to your
|
|
6
|
+
iOS Home app. Since Siri is integrated with the Home app, you can start voice-control your
|
|
7
|
+
accessories right away - e.g. "What is the temperature in my bedroom." and "Turn on the
|
|
8
|
+
lights in the Dining Room."
|
|
9
|
+
|
|
10
|
+
The project was developed for a Raspberry Pi, but it should work on other platforms. You
|
|
11
|
+
can even integrate with HAP-python remotely using HTTP (see below). To kick-start things,
|
|
12
|
+
you can open `main.py`, where you can find out how to launch a mock temperature sensor.
|
|
13
|
+
Just run `python3 main.py` and you should see it in the Home app (be sure to be in the same network).
|
|
14
|
+
Stop it by hitting Ctrl+C.
|
|
15
|
+
|
|
16
|
+
There are example accessories in [the accessories folder](pyhap/accessories) (e.g. AM2302 temperature and humidity sensor).
|
|
17
|
+
|
|
18
|
+
## Table of Contents
|
|
19
|
+
1. [API](#API)
|
|
20
|
+
2. [Installation](#Installation)
|
|
21
|
+
3. [Integrating non-compatible devices](#HttpAcc)
|
|
22
|
+
4. [Run at boot (and a Switch to shutdown your device)](#AtBoot)
|
|
23
|
+
5. [Notice](#Notice)
|
|
24
|
+
|
|
25
|
+
## Installation <a name="Installation"></a>
|
|
26
|
+
|
|
27
|
+
As a prerequisite, you will need Avahi/Bonjour installed (due to zeroconf package).
|
|
28
|
+
On a Raspberry Pi, you can get it with:
|
|
29
|
+
```
|
|
30
|
+
sudo apt-get install libavahi-compat-libdnssd-dev
|
|
31
|
+
```
|
|
32
|
+
`avahi-utils` may also fit the bill. Then, you can install with `pip3` (you will need `sudo` or `--user` for the install):
|
|
33
|
+
```sh
|
|
34
|
+
pip3 install HAP-python
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
This will install HAP-python in your python packages, so that you can import it as `pyhap`. To uninstall, just do:
|
|
38
|
+
```
|
|
39
|
+
pip3 uninstall HAP-python
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API <a name="API"></a>
|
|
43
|
+
|
|
44
|
+
A typical flow for using HAP-python starts with implementing an Accessory. This is done by
|
|
45
|
+
subclassing [Accessory](pyhap/accessory.py) and putting in place a few details
|
|
46
|
+
(see below). After that, you give your accessory to an AccessoryDriver to manage. This
|
|
47
|
+
will take care of advertising it on the local network, setting a HAP server and
|
|
48
|
+
running the Accessory. Take a look at [main.py](main.py) for a quick start on that.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from pyhap.accessory import Accessory, Category
|
|
52
|
+
import pyhap.loader as loader
|
|
53
|
+
|
|
54
|
+
class TemperatureSensor(Accessory):
|
|
55
|
+
"""Implementation of a mock temperature sensor accessory."""
|
|
56
|
+
|
|
57
|
+
category = Category.SENSOR # This is for the icon in the iOS Home app.
|
|
58
|
+
|
|
59
|
+
def __init__(self, *args, **kwargs):
|
|
60
|
+
"""Here, we just store a reference to the current temperature characteristic and
|
|
61
|
+
add a method that will be executed every time its value changes.
|
|
62
|
+
"""
|
|
63
|
+
# If overriding this method, be sure to call the super's implementation first,
|
|
64
|
+
# because it calls _set_services and performs some other important actions.
|
|
65
|
+
super(TemperatureSensor, self).__init__(*args, **kwargs)
|
|
66
|
+
|
|
67
|
+
self.temp_char = self.get_service("TemperatureSensor")\
|
|
68
|
+
.get_characteristic("CurrentTemperature")
|
|
69
|
+
# Having a callback is optional, but you can use it to add functionality.
|
|
70
|
+
self.temp_char.setter_callback = self.temperature_changed
|
|
71
|
+
|
|
72
|
+
def temperature_changed(self, value):
|
|
73
|
+
"""This will be called every time the value of the CurrentTemperature Characteristic
|
|
74
|
+
is changed. Use setter_callbacks to react to user actions, e.g. setting the
|
|
75
|
+
lights On could fire some GPIO code to turn on a LED (see pyhap/accessories/LightBulb.py).
|
|
76
|
+
|
|
77
|
+
NOTE: no need to set the value yourself, this is done for you, before the callback
|
|
78
|
+
is called.
|
|
79
|
+
"""
|
|
80
|
+
print("Temperature changed to: ", value)
|
|
81
|
+
|
|
82
|
+
def _set_services(self):
|
|
83
|
+
"""We override this method to add the services that we want our accessory to
|
|
84
|
+
support. Services have "mandatory" characteristics and optional
|
|
85
|
+
characteristics. Mandatory characteristics are automatically added
|
|
86
|
+
to your selected service, but you must add optional characteristics yourself.
|
|
87
|
+
Take a look at pyhap/accessories/FakeFan.py for an example of how to do that.
|
|
88
|
+
"""
|
|
89
|
+
super(TemperatureSensor, self)._set_services() # Adds some neccessary characteristics.
|
|
90
|
+
# A loader creates Service and Characteristic objects based on json representation
|
|
91
|
+
# such as the Apple-defined ones in pyhap/resources/.
|
|
92
|
+
temp_sensor_service = loader.get_serv_loader().get("TemperatureSensor")
|
|
93
|
+
self.add_service(temp_sensor_service)
|
|
94
|
+
|
|
95
|
+
def run(self):
|
|
96
|
+
"""We override this method to implement what the accessory will do when it is
|
|
97
|
+
started. An accessory is started and stopped from the AccessoryDriver.
|
|
98
|
+
|
|
99
|
+
It might be convenient to use the Accessory's run_sentinel, which is a
|
|
100
|
+
threading.Event object which is set when the accessory should stop running.
|
|
101
|
+
|
|
102
|
+
In this example, we wait 3 seconds to see if the run_sentinel will be set and if
|
|
103
|
+
not, we set the current temperature to a random number.
|
|
104
|
+
"""
|
|
105
|
+
while not self.run_sentinel.wait(3):
|
|
106
|
+
self.temp_char.set_value(random.randint(18, 26))
|
|
107
|
+
|
|
108
|
+
def stop(self):
|
|
109
|
+
"""We override this method to clean up any resources or perform final actions, as
|
|
110
|
+
this is called by the AccessoryDriver when the Accessory is being stopped (it is
|
|
111
|
+
called right after run_sentinel is set).
|
|
112
|
+
"""
|
|
113
|
+
print("Stopping accessory.")
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Integrating non-compatible devices <a name="HttpAcc"></a>
|
|
117
|
+
HAP-python may not be available for many IoT devices. For them, HAP-python allows devices
|
|
118
|
+
to be bridged by means of communicating with an HTTP server - the [HttpBridge](pyhap/accessories/Http.py). You can add as many remote accessories as you like.
|
|
119
|
+
|
|
120
|
+
For example, the bellow snippet creates an Http Accessory that listens on port 51800
|
|
121
|
+
for updates on the TemperatureSensor service:
|
|
122
|
+
```python
|
|
123
|
+
import pyhap.util as util
|
|
124
|
+
import pyhap.loader as loader
|
|
125
|
+
from pyhap.accessories.Http import HttpBridge
|
|
126
|
+
from pyhap.accessory import Accessory
|
|
127
|
+
from pyhap.accessory_driver import AccessoryDriver
|
|
128
|
+
|
|
129
|
+
# get loaders
|
|
130
|
+
service_loader = loader.get_serv_loader()
|
|
131
|
+
char_loader = loader.get_char_loader()
|
|
132
|
+
|
|
133
|
+
# Create an accessory with the temperature sensor service.
|
|
134
|
+
# Also, add an optional characteristic StatusLowBattery to that service.
|
|
135
|
+
remote_accessory = Accessory("foo", aid=2) # aids must be unique for each accessory.
|
|
136
|
+
tservice = service_loader.get("TemperatureSensor")
|
|
137
|
+
tservice.add_opt_characteristic(
|
|
138
|
+
char_loader.get("StatusLowBattery"))
|
|
139
|
+
remote_accessory.add_service(tservice)
|
|
140
|
+
|
|
141
|
+
# Create the HTTP Bridge and add the accessory to it.
|
|
142
|
+
address = ("", 51111)
|
|
143
|
+
http_bridge = HttpBridge(address=address,
|
|
144
|
+
display_name="HTTP Bridge",
|
|
145
|
+
pincode=b"203-23-999")
|
|
146
|
+
http_bridge.add_accessory(remote_accessory)
|
|
147
|
+
|
|
148
|
+
# Add to driver and run.
|
|
149
|
+
driver = AccessoryDriver(http_bridge, 51826)
|
|
150
|
+
driver.start()
|
|
151
|
+
```
|
|
152
|
+
Now, remote accessories can do an HTTP POST to the address of the device where the
|
|
153
|
+
accessory is running (port 51111) with the following content:
|
|
154
|
+
```json
|
|
155
|
+
{
|
|
156
|
+
"aid": 2,
|
|
157
|
+
"services": {
|
|
158
|
+
"TemperatureSensor": {
|
|
159
|
+
"CurrentTemperature" : 20,
|
|
160
|
+
"StatusLowBattery": true,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
This will update the value of the characteristic "CurrentTemperature" to 20 degrees C
|
|
166
|
+
and "StatusLowBattery" to `true`.
|
|
167
|
+
Needless to say the communication to the Http Bridge poses a security risk, so
|
|
168
|
+
keep that in mind.
|
|
169
|
+
|
|
170
|
+
## Run at boot <a name="AtBoot"></a>
|
|
171
|
+
This is a quick way to get `HAP-python` to run at boot on a Raspberry Pi. It is recommended
|
|
172
|
+
to turn on "Wait for network" in `raspi-config`. If this turns to be unreliable, see
|
|
173
|
+
[this](https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=187225).
|
|
174
|
+
|
|
175
|
+
Copy the below in `/etc/systemd/system/HAP-python.service` (needs sudo).
|
|
176
|
+
```
|
|
177
|
+
[Unit]
|
|
178
|
+
Description = HAP-python daemon
|
|
179
|
+
Wants = pigpiod.service # Remove this if you don't depend on pigpiod
|
|
180
|
+
After = local-fs.target network-online.target pigpiod.service
|
|
181
|
+
|
|
182
|
+
[Service]
|
|
183
|
+
User = lesserdaemon # It's a good idea to use some unprivileged system user
|
|
184
|
+
# Script starting HAP-python, e.g. main.py
|
|
185
|
+
# Be careful to set any paths you use, e.g. for persisting the state.
|
|
186
|
+
ExecStart = /usr/bin/python3 /home/lesserdaemon/.hap-python/hap-python.py
|
|
187
|
+
|
|
188
|
+
[Install]
|
|
189
|
+
WantedBy = multi-user.target
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Test that everything is fine by doing:
|
|
193
|
+
|
|
194
|
+
```sh
|
|
195
|
+
> sudo systemctl start HAP-python
|
|
196
|
+
> systemctl status HAP-python
|
|
197
|
+
> sudo journalctl -u HAP-python # to see the output of the start up script.
|
|
198
|
+
> sudo systemctl stop HAP-python
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
To enable or disable at boot, do:
|
|
202
|
+
|
|
203
|
+
```sh
|
|
204
|
+
> sudo systemctl enable HAP-python
|
|
205
|
+
> sudo systemctl disable HAP-python
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Shutdown switch
|
|
209
|
+
|
|
210
|
+
If you are running `HAP-python` on a Raspberry Pi, you may want to add a
|
|
211
|
+
[Shutdown Switch](pyhap/accessories/ShutdownSwitch.py) to your Home. This is a
|
|
212
|
+
Switch Accessory, which, when triggered, executes `sudo shutdown -h now`, i.e.
|
|
213
|
+
it shutdowns and halts the Pi. This allows you to safely unplug it.
|
|
214
|
+
|
|
215
|
+
For the above to work, you need to enable passwordless `/sbin/shutdown` to whichever
|
|
216
|
+
user is running `HAP-python`. For example, do:
|
|
217
|
+
```sh
|
|
218
|
+
$ sudo visudo # and add the line: "<hap-user> ALL=NOPASSWD: /sbin/shutdown".
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Notice <a name="Notice"></a>
|
|
222
|
+
|
|
223
|
+
Some HAP know-how was taken from [HAP-NodeJS by KhaosT](https://github.com/KhaosT/HAP-NodeJS).
|
|
224
|
+
|
|
225
|
+
I am not aware of any bugs, but I am more than confident that such exist. If you find any,
|
|
226
|
+
please report and I will try to fix them.
|
|
227
|
+
|
|
228
|
+
Suggestions are always welcome.
|
|
229
|
+
|
|
230
|
+
Have fun!
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""
|
|
2
|
+
An Accessory wrapper for the SDS011 air particulate density sensor.
|
|
3
|
+
|
|
4
|
+
The density sensor implementation can be found here:
|
|
5
|
+
https://github.com/ikalchev/py-sds011
|
|
6
|
+
Place the file under a package named sensors in your python path,
|
|
7
|
+
or change the import alltogether.
|
|
8
|
+
"""
|
|
9
|
+
import time
|
|
10
|
+
import logging
|
|
11
|
+
|
|
12
|
+
from sensors.SDS011 import SDS011 as AirSensor
|
|
13
|
+
from pyhap.accessory import Accessory, Category
|
|
14
|
+
import pyhap.loader as loader
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SDS011(Accessory):
|
|
20
|
+
"""Accessory wrapper for SDS011.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
category = Category.SENSOR
|
|
24
|
+
|
|
25
|
+
SORTED_PM_QUALITY_MAP = ((200, 5), (150, 4), (100, 3), (50, 2), (0, 1))
|
|
26
|
+
"""
|
|
27
|
+
Threshold-to-state tuples. These show the state for which the threshold is
|
|
28
|
+
lower boundry. Uses something like Air Quality Index (AQI).
|
|
29
|
+
|
|
30
|
+
The UI shows:
|
|
31
|
+
1 - Excellent
|
|
32
|
+
2 - Good
|
|
33
|
+
3 - Fair
|
|
34
|
+
4 - Inferior
|
|
35
|
+
5 - Poor
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, serial_port, *args, sleep_duration_s=15*60, calib_duration_s=15,
|
|
39
|
+
**kwargs):
|
|
40
|
+
"""Initialise and start SDS011 on the given port.
|
|
41
|
+
|
|
42
|
+
@param serial_port: The SDS011 port, e.g. /dev/ttyUSB0.
|
|
43
|
+
@type serial_port: str
|
|
44
|
+
|
|
45
|
+
@param sleep_duration_s: The amount of time in seconds the sensor sleeps before
|
|
46
|
+
waking it up for reading. Defaults to 15 minutes.
|
|
47
|
+
@type sleep_duration_s: float
|
|
48
|
+
|
|
49
|
+
@param calib_duration_s: The amount of time in seconds to wait after the sensor
|
|
50
|
+
has been waken up before reading its data. Defaults to 15 seconds.
|
|
51
|
+
@type calib_duration_s: float
|
|
52
|
+
"""
|
|
53
|
+
self.pm25_quality = None
|
|
54
|
+
self.pm25_density = None
|
|
55
|
+
self.pm10_quality = None
|
|
56
|
+
self.pm10_density = None
|
|
57
|
+
super(SDS011, self).__init__(*args, **kwargs)
|
|
58
|
+
self.sleep_duration_s = sleep_duration_s
|
|
59
|
+
self.calib_duration_s = calib_duration_s
|
|
60
|
+
self.serial_port = serial_port
|
|
61
|
+
self.sensor = AirSensor(serial_port)
|
|
62
|
+
self.sensor.sleep(sleep=False)
|
|
63
|
+
|
|
64
|
+
def _set_services(self):
|
|
65
|
+
"""Add the AirQualitySensor services (one for PM2.5 and one for PM10).
|
|
66
|
+
|
|
67
|
+
Also adds and configures optional characteristics, such as Name,
|
|
68
|
+
AirParticulateSize, AirParticulateDensity.
|
|
69
|
+
"""
|
|
70
|
+
super(SDS011, self)._set_services()
|
|
71
|
+
char_loader = loader.get_char_loader()
|
|
72
|
+
|
|
73
|
+
# PM2.5
|
|
74
|
+
air_quality_pm25 = loader.get_serv_loader().get("AirQualitySensor")
|
|
75
|
+
pm25_size = char_loader.get("AirParticulateSize")
|
|
76
|
+
pm25_size.set_value(0, should_notify=False)
|
|
77
|
+
self.pm25_density = char_loader.get("AirParticulateDensity")
|
|
78
|
+
pm25_name = char_loader.get("Name")
|
|
79
|
+
pm25_name.set_value("PM2.5", should_notify=False)
|
|
80
|
+
self.pm25_quality = air_quality_pm25.get_characteristic("AirQuality")
|
|
81
|
+
air_quality_pm25.add_opt_characteristic(pm25_name, pm25_size, self.pm25_density)
|
|
82
|
+
|
|
83
|
+
# PM10
|
|
84
|
+
air_quality_pm10 = loader.get_serv_loader().get("AirQualitySensor")
|
|
85
|
+
pm10_size = char_loader.get("AirParticulateSize")
|
|
86
|
+
pm10_size.set_value(1, should_notify=False)
|
|
87
|
+
self.pm10_density = char_loader.get("AirParticulateDensity")
|
|
88
|
+
pm10_name = char_loader.get("Name")
|
|
89
|
+
pm10_name.set_value("PM10", should_notify=False)
|
|
90
|
+
self.pm10_quality = air_quality_pm10.get_characteristic("AirQuality")
|
|
91
|
+
air_quality_pm10.add_opt_characteristic(pm10_name, pm10_size, self.pm10_density)
|
|
92
|
+
|
|
93
|
+
self.add_service(air_quality_pm25)
|
|
94
|
+
self.add_service(air_quality_pm10)
|
|
95
|
+
|
|
96
|
+
def __getstate__(self):
|
|
97
|
+
"""Get the state, less the sensor.
|
|
98
|
+
"""
|
|
99
|
+
state = super(SDS011, self).__getstate__()
|
|
100
|
+
state["sensor"] = None
|
|
101
|
+
return state
|
|
102
|
+
|
|
103
|
+
def __setstate__(self, state):
|
|
104
|
+
"""Set the state of this Accessory and initialise the sensor
|
|
105
|
+
with the serial port in the state.
|
|
106
|
+
"""
|
|
107
|
+
self.__dict__.update(state)
|
|
108
|
+
self.sensor = AirSensor(self.serial_port)
|
|
109
|
+
|
|
110
|
+
def get_quality_classification(self, pm, is_pm25=False):
|
|
111
|
+
"""Get the air quality clasification based on the PM density.
|
|
112
|
+
|
|
113
|
+
Uses Air Quality Index (AQI), without averaging for an hour.
|
|
114
|
+
|
|
115
|
+
@see: SDS011.SORTED_PM_QUALITY_MAP
|
|
116
|
+
|
|
117
|
+
@rtype: int
|
|
118
|
+
"""
|
|
119
|
+
assert pm >= 0
|
|
120
|
+
return next(state for threshold, state in self.SORTED_PM_QUALITY_MAP
|
|
121
|
+
if threshold <= pm)
|
|
122
|
+
|
|
123
|
+
def run(self):
|
|
124
|
+
"""Start updating the air quality readings.
|
|
125
|
+
|
|
126
|
+
Initially, we read from the sensor and update the values. Then we put
|
|
127
|
+
it in sleep mode and while the sentinel is not set:
|
|
128
|
+
- Sleep for `self.sleep_duration_s`.
|
|
129
|
+
- Wake up and wait `self.calib_duration_s` seconds.
|
|
130
|
+
- Get the sensor's readings and update.
|
|
131
|
+
"""
|
|
132
|
+
pm25, pm10 = self.sensor.query()
|
|
133
|
+
self.pm25_density.set_value(pm25)
|
|
134
|
+
self.pm25_quality.set_value(
|
|
135
|
+
self.get_quality_classification(pm25, is_pm25=True))
|
|
136
|
+
self.pm10_density.set_value(pm10)
|
|
137
|
+
self.pm10_quality.set_value(
|
|
138
|
+
self.get_quality_classification(pm10, is_pm25=False))
|
|
139
|
+
self.sensor.sleep()
|
|
140
|
+
while not self.run_sentinel.wait(self.sleep_duration_s):
|
|
141
|
+
logger.debug("Waking up sensor.")
|
|
142
|
+
self.sensor.sleep(sleep=False)
|
|
143
|
+
time.sleep(self.calib_duration_s)
|
|
144
|
+
pm25, pm10 = self.sensor.query()
|
|
145
|
+
self.pm25_density.set_value(pm25)
|
|
146
|
+
self.pm25_quality.set_value(
|
|
147
|
+
self.get_quality_classification(pm25, is_pm25=True))
|
|
148
|
+
self.pm10_density.set_value(pm10)
|
|
149
|
+
self.pm10_quality.set_value(
|
|
150
|
+
self.get_quality_classification(pm10, is_pm25=False))
|
|
151
|
+
self.sensor.sleep()
|
|
152
|
+
logger.debug("Read cycle done. Sleeping.")
|
|
@@ -146,7 +146,6 @@ class Accessory(object):
|
|
|
146
146
|
The default implementation adds only the AccessoryInformation services
|
|
147
147
|
and sets its Name characteristic to the Accessory's display name.
|
|
148
148
|
"""
|
|
149
|
-
# Info service
|
|
150
149
|
info_service = get_serv_loader().get("AccessoryInformation")
|
|
151
150
|
info_service.get_characteristic("Name")\
|
|
152
151
|
.set_value(self.display_name, False)
|
|
@@ -172,6 +171,22 @@ class Accessory(object):
|
|
|
172
171
|
"""
|
|
173
172
|
self.run_sentinel = run_sentinel
|
|
174
173
|
|
|
174
|
+
def config_changed(self):
|
|
175
|
+
"""Notify the accessory about configuration changes.
|
|
176
|
+
|
|
177
|
+
These include new services or updated characteristic values, e.g.
|
|
178
|
+
the Name of a service changed.
|
|
179
|
+
|
|
180
|
+
This method also notifies the broker about the change, so that it can
|
|
181
|
+
publish the changes to the world.
|
|
182
|
+
|
|
183
|
+
@note: If you are changing the configuration of a bridged accessory
|
|
184
|
+
(i.e. an Accessory that is contained in a Bridge),
|
|
185
|
+
you should call the `config_changed` method on the Bridge.
|
|
186
|
+
"""
|
|
187
|
+
self.config_version += 1
|
|
188
|
+
self.broker.config_changed()
|
|
189
|
+
|
|
175
190
|
def add_service(self, *servs):
|
|
176
191
|
"""Add the given services to this Accessory.
|
|
177
192
|
|
|
@@ -12,7 +12,8 @@ AccessoryDriver (all this happens through the publish() interface). The Accessor
|
|
|
12
12
|
will then check if there is a client that subscribed for events from this exact
|
|
13
13
|
Characteristic from this exact Accessory (remember, it could be a Bridge with more than
|
|
14
14
|
one Accessory in it). If so, the event is put in a FIFO queue - the event queue. This
|
|
15
|
-
terminates the call chain and concludes the publishing process from the Characteristic
|
|
15
|
+
terminates the call chain and concludes the publishing process from the Characteristic,
|
|
16
|
+
the Characteristic does not block waiting for the actual send to happen.
|
|
16
17
|
|
|
17
18
|
When the AccessoryDriver is started, it spawns an event dispatch thread. The purpose of
|
|
18
19
|
this thread is to get events from the event queue and send them to subscribed clients.
|
|
@@ -217,6 +218,16 @@ class AccessoryDriver(object):
|
|
|
217
218
|
self.sent_events = 0
|
|
218
219
|
self.accumulated_qsize = 0
|
|
219
220
|
|
|
221
|
+
def config_changed(self):
|
|
222
|
+
"""Notify the driver that the accessory's configuration has changed.
|
|
223
|
+
|
|
224
|
+
Updates the mDNS advertisment, so that iOS clients know they need to new data.
|
|
225
|
+
Also, persists the accessory, so that the new configuration is available on
|
|
226
|
+
restart.
|
|
227
|
+
"""
|
|
228
|
+
self.update_advertisment()
|
|
229
|
+
self.persist()
|
|
230
|
+
|
|
220
231
|
def update_advertisment(self):
|
|
221
232
|
"""Updates the mDNS service info for the accessory."""
|
|
222
233
|
self.advertiser.unregister_service(self.mdns_service_info)
|
|
@@ -434,6 +445,10 @@ class AccessoryDriver(object):
|
|
|
434
445
|
self.http_server.shutdown()
|
|
435
446
|
self.http_server.server_close()
|
|
436
447
|
self.http_server_thread.join()
|
|
448
|
+
|
|
449
|
+
logger.debug("Persisting accessory state")
|
|
450
|
+
self.persist()
|
|
451
|
+
|
|
437
452
|
logger.debug("AccessoryDriver stopped successfully")
|
|
438
453
|
|
|
439
454
|
def signal_handler(self, _signal, _frame):
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
"""
|
|
2
|
+
All things for a HAP characteristic.
|
|
2
3
|
|
|
4
|
+
A Characteristic is the smallest unit of the smart home, e.g.
|
|
5
|
+
a temperature measuring or a device status.
|
|
6
|
+
"""
|
|
3
7
|
|
|
4
8
|
class HAP_FORMAT:
|
|
5
9
|
BOOL = 'bool'
|
|
@@ -52,39 +56,59 @@ class CharacteristicError(Exception):
|
|
|
52
56
|
pass
|
|
53
57
|
|
|
54
58
|
|
|
59
|
+
_HAP_NUMERIC_FIELDS = {"maxValue", "minValue", "minStep", "unit"}
|
|
55
60
|
"""Fields that should be included in the HAP representation of the characteristic.
|
|
56
61
|
|
|
57
62
|
That is, if they are present in the specification of a numeric-value characteristic.
|
|
58
63
|
"""
|
|
59
|
-
_HAP_NUMERIC_FIELDS = {"maxValue", "minValue", "minStep", "unit"}
|
|
60
64
|
|
|
61
65
|
|
|
62
66
|
class Characteristic(object):
|
|
67
|
+
"""Represents a HAP characteristic, the smallest unit of the smart home.
|
|
68
|
+
|
|
69
|
+
A HAP characteristic is some measurement or state, like battery status or
|
|
70
|
+
the current temperature. Characteristics are contained in services.
|
|
71
|
+
Each charactaristic has a unique type UUID and a set of properties,
|
|
72
|
+
like format, min and max values, valid values and others.
|
|
73
|
+
"""
|
|
63
74
|
|
|
64
75
|
def __init__(self, display_name, type_id, properties, value=None, broker=None):
|
|
76
|
+
assert "Format" in properties and "Permissions" in properties
|
|
65
77
|
self.display_name = display_name
|
|
66
78
|
self.type_id = type_id
|
|
67
|
-
assert "Format" in properties and "Permissions" in properties
|
|
68
79
|
self.properties = properties
|
|
69
|
-
self.
|
|
80
|
+
self.has_valid_values = "ValidValues" in self.properties
|
|
70
81
|
self.value = value or HAP_FORMAT.DEFAULT[properties["Format"]]
|
|
71
82
|
self.broker = broker
|
|
72
83
|
self.setter_callback = None
|
|
73
|
-
self.
|
|
84
|
+
self.hap_template = self._create_hap_template()
|
|
85
|
+
|
|
86
|
+
def _create_hap_template(self):
|
|
87
|
+
"""Create a HAP template for describing this Characteristic.
|
|
88
|
+
|
|
89
|
+
Contains properties that do not change or change rarely, e.g. the type.
|
|
90
|
+
"""
|
|
91
|
+
template = dict()
|
|
92
|
+
if self.properties["Format"] in HAP_FORMAT.NUMERIC:
|
|
93
|
+
template = {k: self.properties[k]
|
|
94
|
+
for k in self.properties.keys() & _HAP_NUMERIC_FIELDS}
|
|
95
|
+
return template
|
|
74
96
|
|
|
75
97
|
def set_value(self, value, should_notify=True):
|
|
76
|
-
"""Set the given value.
|
|
98
|
+
"""Set the given raw value. It is checked if it is a valid value.
|
|
77
99
|
|
|
78
100
|
@param value: The value to assign as this Characteristic's value.
|
|
79
101
|
@type value: Depends on properties["Format"]
|
|
80
102
|
|
|
81
103
|
@param should_notify: Whether a the change should be sent to subscribed clients.
|
|
104
|
+
The notification is called _after_ the setter callback.
|
|
82
105
|
@type should_notify: bool
|
|
83
106
|
|
|
84
|
-
@raise ValueError: When the value being assigned is not one of the
|
|
107
|
+
@raise ValueError: When the value being assigned is not one of the valid values
|
|
85
108
|
for this Characteristic.
|
|
86
109
|
"""
|
|
87
|
-
if self.
|
|
110
|
+
if (self.has_valid_values
|
|
111
|
+
and value not in self.properties["ValidValues"].values()):
|
|
88
112
|
raise ValueError
|
|
89
113
|
self.value = value
|
|
90
114
|
if self.setter_callback is not None:
|
|
@@ -93,53 +117,49 @@ class Characteristic(object):
|
|
|
93
117
|
self.notify()
|
|
94
118
|
|
|
95
119
|
def get_value(self):
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def notify(self):
|
|
99
|
-
data = {
|
|
100
|
-
"type_id": self.type_id,
|
|
101
|
-
"value": self.value,
|
|
102
|
-
}
|
|
103
|
-
self.broker.publish(data, self)
|
|
120
|
+
"""Get the raw value of this Characteristic.
|
|
104
121
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
template = {k: self.properties[k]
|
|
109
|
-
for k in self.properties.keys() & _HAP_NUMERIC_FIELDS}
|
|
110
|
-
return template
|
|
111
|
-
|
|
112
|
-
def _value_to_HAP(self):
|
|
113
|
-
hap_rep = self.hap_value_template.copy()
|
|
122
|
+
@deprecated: Use self.value instead.
|
|
123
|
+
"""
|
|
124
|
+
return self.value
|
|
114
125
|
|
|
126
|
+
def get_hap_value(self):
|
|
127
|
+
"""Get the value of the characteristic, constrained with the HAP properties.
|
|
128
|
+
"""
|
|
129
|
+
val = self.value
|
|
115
130
|
if self.properties["Format"] == HAP_FORMAT.STRING:
|
|
116
|
-
val =
|
|
117
|
-
if len(self.value) > 64:
|
|
118
|
-
hap_rep["maxLen"] = min(len(self.value), 256)
|
|
131
|
+
val = val[:256]
|
|
119
132
|
elif self.properties["Format"] in HAP_FORMAT.NUMERIC:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
else:
|
|
126
|
-
val = self.value
|
|
133
|
+
if "maxValue" in self.properties:
|
|
134
|
+
val = min(self.properties["maxValue"], val)
|
|
135
|
+
if "minValue" in self.properties:
|
|
136
|
+
val = max(self.properties["minValue"], val)
|
|
137
|
+
return val
|
|
127
138
|
|
|
128
|
-
|
|
129
|
-
|
|
139
|
+
def notify(self):
|
|
140
|
+
"""Notify clients about a value change.
|
|
130
141
|
|
|
131
|
-
|
|
142
|
+
@note: Non-blocking, i.e. does not wait for the update to be sent.
|
|
143
|
+
@note: Uses the `get_hap_value`, i.e. sends the HAP value.
|
|
144
|
+
@see: accessory_driver.publish
|
|
145
|
+
"""
|
|
146
|
+
data = {
|
|
147
|
+
"type_id": self.type_id,
|
|
148
|
+
"value": self.get_hap_value(),
|
|
149
|
+
}
|
|
150
|
+
self.broker.publish(data, self)
|
|
132
151
|
|
|
133
|
-
def to_HAP(self, iid_manager
|
|
152
|
+
def to_HAP(self, iid_manager):
|
|
134
153
|
"""Create a HAP representation of this Characteristic.
|
|
135
154
|
|
|
136
|
-
@
|
|
137
|
-
|
|
155
|
+
@note: Uses the `get_hap_value`, i.e. sends the HAP value.
|
|
156
|
+
|
|
157
|
+
@param iid_manager: IID manager to query for this object's IID.
|
|
158
|
+
@type iid_manager: IIDManager
|
|
138
159
|
|
|
139
160
|
@return: A HAP representation.
|
|
140
161
|
@rtype: dict
|
|
141
162
|
"""
|
|
142
|
-
assert iid_manager is not None
|
|
143
163
|
hap_rep = {
|
|
144
164
|
"iid": iid_manager.get_iid(self),
|
|
145
165
|
"type": str(self.type_id).upper(),
|
|
@@ -147,5 +167,14 @@ class Characteristic(object):
|
|
|
147
167
|
"perms": self.properties["Permissions"],
|
|
148
168
|
"format": self.properties["Format"],
|
|
149
169
|
}
|
|
150
|
-
|
|
170
|
+
|
|
171
|
+
value_info = self.hap_template.copy()
|
|
172
|
+
val = self.get_hap_value()
|
|
173
|
+
if self.properties["Format"] == HAP_FORMAT.STRING:
|
|
174
|
+
if len(val) > 64:
|
|
175
|
+
value_info["maxLen"] = min(len(val), 256)
|
|
176
|
+
if HAP_PERMISSIONS.READ in self.properties["Permissions"]:
|
|
177
|
+
value_info["value"] = val
|
|
178
|
+
|
|
179
|
+
hap_rep.update(value_info)
|
|
151
180
|
return hap_rep
|
|
File without changes
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from setuptools import setup
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="HAP-python",
|
|
5
|
+
description="HomeKit Accessory Protocol implementation in python3",
|
|
6
|
+
author="Ivan Kalchev",
|
|
7
|
+
version="1.0.7",
|
|
8
|
+
url="https://github.com/ikalchev/HAP-python.git",
|
|
9
|
+
classifiers=[
|
|
10
|
+
"Development Status :: 5 - Production/Stable",
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Operating System :: OS Independent",
|
|
13
|
+
"Topic :: Home Automation",
|
|
14
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
15
|
+
"License :: OSI Approved :: Apache Software License",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
],
|
|
18
|
+
license="Apache-2.0",
|
|
19
|
+
packages=[
|
|
20
|
+
"pyhap",
|
|
21
|
+
"pyhap.accessories"
|
|
22
|
+
],
|
|
23
|
+
install_requires=[
|
|
24
|
+
"pycryptodome",
|
|
25
|
+
"tlslite-ng",
|
|
26
|
+
"ed25519",
|
|
27
|
+
"zeroconf",
|
|
28
|
+
"curve25519-donna"
|
|
29
|
+
],
|
|
30
|
+
extras_require={
|
|
31
|
+
"dev": [
|
|
32
|
+
"pytest",
|
|
33
|
+
"tox",
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
package_data={
|
|
37
|
+
"pyhap": ["resources/*"],
|
|
38
|
+
}
|
|
39
|
+
)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/LightBulb.cpython-36.pyc
DELETED
|
Binary file
|
usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/MotionSensor.cpython-36.pyc
DELETED
|
Binary file
|
usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/ShutdownSwitch.cpython-36.pyc
DELETED
|
Binary file
|
|
Binary file
|
usr/local/lib/python3.6/site-packages/pyhap/accessories/__pycache__/TemperatureSensor.cpython-36.pyc
DELETED
|
Binary file
|
|
Binary file
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/MotionSensor.py
RENAMED
|
File without changes
|
{usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/ShutdownSwitch.py
RENAMED
|
File without changes
|
|
File without changes
|
{usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/accessories/TemperatureSensor.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{usr/local/lib/python3.6/site-packages → HAP-python-1.0.7}/pyhap/resources/characteristics.json
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|