Skip to content

Commit 1ede9ae

Browse files
committed
finish OpenHAB tutorial
- add DHT11 example - revise flow of examples - improve docs for HomieDevice class attrs
1 parent 2edfa62 commit 1ede9ae

File tree

14 files changed

+220
-51
lines changed

14 files changed

+220
-51
lines changed

circuitpython_homie/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,13 @@ class HomieDevice:
297297
the given input.
298298
"""
299299

300-
#: The :homie-attr:`implementation` attribute is global to all instantiated devices.
301300
implementation = "CircuitPython on " + uname()[0]
301+
"""The :homie-attr:`implementation` attribute used for all `HomieDevice` instances
302+
(class attribute). The platform specified by default is taken from
303+
:attr:`~os._Uname.sysname`.
304+
"""
302305

303-
#: The base topic used for all Homie devices.
306+
#: The base topic used for all `HomieDevice` instances (class attribute).
304307
base_topic = "homie"
305308

306309
def __init__(self, client: MQTT, name: str, device_id: str):

docs/_static/extra_css.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ a code.oh {
6363

6464
code.oh-red,
6565
a code.oh-red {
66-
background-color: hsl(14, 100%, 50%);
66+
background-color: hsl(3, 100%, 59%);
67+
}
68+
69+
code.oh-orange {
70+
background-color: hsl(14, 80%, 50%);
6771
}
6872

6973
code.oh-green {
3.6 KB
Loading
3.42 KB
Loading
3.76 KB
Loading
3.67 KB
Loading

docs/examples.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ Demonstrates using the `PropertyPercent` class to represent a light sensor's dat
6666
:start-after: pylint: disable=import-error
6767
:lineno-match:
6868

69+
DHT Sensor test
70+
-----------------
71+
72+
Demonstrates using the `PropertyPercent` and `PropertyFloat` classes to represent a DHT11 sensor's
73+
humidity and temperature data (respectively).
74+
75+
.. literalinclude:: ../examples/homie_dht_sensor_test.py
76+
:caption: examples/homie_dht_sensor_test.py
77+
:linenos:
78+
:start-after: pylint: disable=import-error
79+
:lineno-match:
80+
6981
Clock test
7082
------------
7183

docs/tutorials/openhab.rst

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
:class: oh-green oh
66
.. role:: oh-blue(literal)
77
:class: oh-blue oh
8+
.. role:: oh-orange(literal)
9+
:class: oh-orange oh
10+
11+
.. _MQTT binding: https://www.openhab.org/addons/bindings/mqtt/
12+
.. _MQTT Homie binding: https://www.openhab.org/addons/bindings/mqtt.homie/
13+
.. _Simple test: ../examples.html#simple-test
14+
.. |click| replace:: Click or tap
15+
.. |oh-thing| replace:: OpenHAB Thing
16+
.. |oh-item| replace:: OpenHAB Item
17+
.. |homie-dev| replace:: Homie Device
818

919
Using Homie with OpenHAB_
1020
=========================
@@ -37,10 +47,6 @@ the interface. The rest of this tutorial will assume that you are logged into th
3747
with an OpenHAB administrator account. This should have been covered in the
3848
`OpenHAB Getting Started instructions`_
3949

40-
.. _MQTT binding: https://www.openhab.org/addons/bindings/mqtt/
41-
.. _MQTT Homie binding: https://www.openhab.org/addons/bindings/mqtt.homie/
42-
.. |click| replace:: Click or tap
43-
4450
.. tip::
4551
Some of the images here are hyperlinked to the http://openhabian:8080 domain for quicker access.
4652
If you are using a different hostname or a static IP address, then you can adjust the address in
@@ -70,38 +76,38 @@ you don't see the `MQTT binding`_.
7076
|click| the :homie-val:`install` button to install the binding and add MQTT capability to your
7177
OpenHAB server.
7278

73-
.. md-admonition::
74-
:class: info
75-
76-
`Installing the MQTT binding`_ in OpenHAB will also install Homie support automatically. More info
77-
about Homie support can be found at the `MQTT Homie binding`_ page.
78-
79-
.. md-admonition::
80-
:class: missing
79+
`Installing the MQTT binding`_ in OpenHAB will also install Homie support automatically. More info
80+
about Homie support can be found at the `MQTT Homie binding`_ page.
8181

82-
The OpenHAB `MQTT Homie binding`_ will say that it supports Homie v3.x specifications. This library
83-
implements Homie v4 specifications. Homie v4 is mostly backward compatible with Homie v3 with
84-
the following exceptions:
82+
.. admonition:: Homie v3 vs Homie v4
83+
:class: missing
84+
:name: v3-vs-v4
8585

86-
- `Node Arrays <https://homieiot.github.io/specification/spec-core-v3_0_1/#arrays>`_
87-
are not supported in Homie v4
88-
- `Device Statistics <https://homieiot.github.io/specification/spec-core-v3_0_1/#device-statistics>`_
89-
are not supported in Homie v4
86+
The OpenHAB `MQTT Homie binding`_ will say that it supports Homie v3.x specifications. This library
87+
implements Homie v4 specifications. Homie v4 is mostly backward compatible with Homie v3 with
88+
the following exceptions:
9089

91-
These missing features are memory and process intensive for microcontrollers. At this time,
92-
there are no plans to add Homie v3 support for this library.
90+
- `Node Arrays <https://homieiot.github.io/specification/spec-core-v3_0_1/#arrays>`_
91+
are not supported in Homie v4. Incidentally, Arrays aren't implemented in OpenHAB's
92+
`MQTT Homie binding`_ because the Homie specification was too vague which is why it was
93+
removed in Homie v4 (see `this HomieIoT thread comment
94+
<https://github.com/homieiot/convention/issues/90#issuecomment-385425001>`_).
95+
- `Device Statistics <https://homieiot.github.io/specification/spec-core-v3_0_1/#device-statistics>`_
96+
are not supported in Homie v4. This was removed in Homie v4 in favor of using nodes' properties
97+
(see `this HomieIoT discussion <https://github.com/homieiot/convention/issues/102>`_).
9398

94-
.. |OH-thing| replace:: OpenHAB Thing
99+
These missing features are memory and process intensive for microcontrollers. At this time,
100+
there is no plan to add Homie v3 support for this library.
95101

96102
.. _add_broker_as_thing:
97103

98-
Adding the MQTT broker as an |OH-thing|
104+
Adding the MQTT broker as an |oh-thing|
99105
***************************************
100106

101107
After `Installing the MQTT binding`_, navigate back to the settings page and open
102108
`the "Things" category <http://openhabian:8080/settings/things/>`_. You may think that installing
103109
the MQTT binding didn't change anything, but automatic discovery of MQTT-capable devices still
104-
requires an |OH-thing| to represent the MQTT broker.
110+
requires an |oh-thing| to represent the MQTT broker.
105111

106112
.. |OH_plus| replace:: :oh-blue:`+`
107113
.. _OH_plus: http://openhabian:8080/settings/things/add
@@ -116,7 +122,7 @@ requires an |OH-thing| to represent the MQTT broker.
116122
.. image:: ../_static/tutorial_images/mqtt_binding_thing-dark.png
117123
:class: only-dark align-center
118124
:target: http://openhabian:8080/settings/things/mqtt
119-
3. At the top of the list of options that you can add as |OH-thing|\ s, you should see the MQTT broker option.
125+
3. At the top of the list of options that you can add as |oh-thing|\ s, you should see the MQTT broker option.
120126
It will have a badge on it that says :oh-blue:`Bridge`. |click| on the MQTT broker option.
121127

122128
.. image:: ../_static/tutorial_images/mqtt_broker_thing-light.png
@@ -175,25 +181,80 @@ requires an |OH-thing| to represent the MQTT broker.
175181
Hover your mouse (or tap and hold) over the :oh-red:`ERROR` badge to see a tooltip briefly
176182
explaining the reason for the error.
177183

178-
Adding a Homie Device as an |OH-thing|
184+
Adding a |homie-dev| as an |oh-thing|
179185
-----------------------------------------
180186

181187
Once you have finished :ref:`add_broker_as_thing`, you are now ready to start using OpenHAB's automatic
182-
discovery of Homie Devices. This section should be repeated for any instantiated `HomieDevice`
188+
discovery of |homie-dev|\ s. This section should be repeated for any instantiated `HomieDevice`
183189
object.
184190

185191
.. note::
186192
Once completed, there is no need to repeat these steps again for the same `HomieDevice` object
187193
unless you have changed the ``device_id`` parameter to the `HomieDevice` constructor. Connecting
188-
& disconnecting a Homie Device that are already added as |OH-thing|\ s should be automatically
194+
& disconnecting a |homie-dev| that are already added as |oh-thing|\ s should be automatically
189195
handled by the OpenHAB `MQTT Homie binding`_.
190196

191197
First lets get a library example running on a circuitPython enabled board (with WiFi support).
192198
See the :doc:`../examples` to understand how to run a library example. For this tutorial, we'll be
193-
using the `Simple test <../examples.html#simple-test>`_ example.
199+
using the `Simple test`_ example.
200+
201+
Once you've got an example running on your circuitpython board, The `HomieDevice` must be added to
202+
OpenHAB as an |oh-thing|. The `HomieProperty` values are used in OpenHAB as a |oh-item|, and each
203+
|oh-item| must be "linked" to an |oh-thing|'s "channel"
204+
205+
1. To see any new Homie devices discovered by the MQTT binding, navigate to
206+
`Settings -> Things <http://openhabian:8080/settings/things/>`_. |click| on the notification
207+
titled :oh-red:`Inbox` at the bottom of the screen.
208+
2. You should see your new `HomieDevice` listed by it's ``device-_id`` (a required parameter in the
209+
`HomieDevice` constructor).
210+
211+
.. image:: ../_static/tutorial_images/discovered_thing-light.png
212+
:class: only-light align-center
213+
.. image:: ../_static/tutorial_images/discovered_thing-dark.png
214+
:class: only-dark align-center
215+
216+
|click| on the discovered |homie-dev| and select :homie-dev:`Add as Thing` from the pop-up menu.
217+
It will ask you for a customized name to be assigned to the |oh-thing|. By default it will use
218+
the ``device_id`` if not changed, so this step is optional. |click| the :oh-orange:`OK` button
219+
when done.
220+
3. You should now see the |homie-dev| in your list of |oh-thing|\ s.
221+
222+
.. image:: ../_static/tutorial_images/homie_thing-light.png
223+
:class: only-light align-center
224+
.. image:: ../_static/tutorial_images/homie_thing-dark.png
225+
:class: only-dark align-center
194226

195-
Once you've got an example running, head over to you OpenHAB dashboard: http://openhabian:8080
196-
(you may have to replace the hostname ``openhabian`` with the IP address like ``192.168.1.xxx``).
197-
To see any new Homie devices discovered by the MQTT binding, navigate to "Settings" -> "Things"
198-
(http://openhabian:8080/settings/things/). At the bottom of the screen, you should see badge-like
199-
notification titled "Inbox".
227+
To use this |homie-dev|'s properties in the OpenHAB user interfaces, you need to create an
228+
|oh-item| for each |homie-dev| property (programmatically instantiated with `HomieProperty` or
229+
:doc:`one of its derivatives <../API/recipes>`). Each |oh-item| must be linked to a |homie-dev|
230+
property via an |oh-thing|'s channel(s).
231+
232+
To see the channels, navigate to the configuration of the |oh-thing| that represents your
233+
|homie-dev| (in your list of |oh-thing|\ s). |click| on the tab named ``Channels`` at the top of
234+
the page.
235+
236+
You should now see a list of properties belonging to your |homie-dev|. Using the `Simple test`_
237+
example, this list only has a ``color`` property. There are various ways to create |oh-item|\ s
238+
from the |oh-thing|'s ``Channels`` configuration page. Choosing 1 will depend on how you wish to
239+
craft your OpenHAB User Interface, Dashboard, or Sitemap.
240+
241+
- |click| on an available channel and select ``Add link to Item...``, then select
242+
``Create a new Item``. This will create a single |oh-item|, but the item's ID must be unique in
243+
OpenHAB (cannot reuse the same ID for multiple |oh-item|\ s linked to the same |oh-thing|'s
244+
channel). While this is the most flexible, it can also become the most tedious.
245+
- |click| on the button titled :homie-val:`Add points to Model`. This will create the necessary
246+
|oh-item|\ (s) and link them to the respective property's channel.
247+
- |click| on the button titled :homie-val:`Add Equipment to Model`. This is similar to
248+
:homie-val:`Add points to Model`, however the created |oh-item|\ (s) are put into a group that
249+
represents a category of equipment.
250+
251+
.. admonition:: Going Forward
252+
:class: check
253+
254+
It is important to understand `OpenHAB's Semantic Model
255+
<https://www.openhab.org/docs/tutorial/model.html>`_ and how they can be used when crafting
256+
a User Interface.
257+
258+
This tutorial does not cover how to use OpenHAB in general. The main point of this tutorial
259+
is how to use the CircuitPython_Homie library for automatic discovery of DIY devices in
260+
OpenHAB.

examples/homie_button_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def change_state(client: MQTT, topic: str, message: str):
7878
def on_disconnected(client: MQTT, user_data, rc):
7979
"""Callback invoked when connection to broker is terminated."""
8080
print("Reconnecting to the broker.")
81-
client.connect()
82-
device.begin()
81+
client.reconnect()
82+
device.set_state("ready")
8383

8484

8585
mqtt_client.on_disconnect = on_disconnected
@@ -111,4 +111,4 @@ def on_disconnected(client: MQTT, user_data, rc):
111111
except KeyboardInterrupt:
112112
device.set_state("disconnected")
113113
mqtt_client.on_disconnect = lambda *args: print("Disconnected from broker")
114-
mqtt_client.disconnect()
114+
mqtt_client.deinit()

examples/homie_clock_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
def on_disconnected(client: MQTT, user_data, rc):
7474
"""Callback invoked when connection to broker is terminated."""
7575
print("Reconnecting to the broker.")
76-
client.connect()
77-
device.begin()
76+
client.reconnect()
77+
device.set_state("ready")
7878

7979

8080
mqtt_client.on_disconnect = on_disconnected
@@ -101,4 +101,4 @@ def on_disconnected(client: MQTT, user_data, rc):
101101
device.set_state("disconnected")
102102
print() # move cursor to next line
103103
mqtt_client.on_disconnect = lambda *args: print("Disconnected from broker")
104-
mqtt_client.disconnect()
104+
mqtt_client.deinit()

0 commit comments

Comments
 (0)