After starting to look into openHAB, and automate the installation with Ansible, I discovered that there is an API, but the main focus of the documentation and examples clearly is on using the UI and don’t automate the installation. It takes some time to figure out the details. In my last blog post I automated the installation of extensions/bindings, and ended with running a rediscover scan.
Now it’s time to see if there are new items in the inbox, and possibly auto-approve and link them in Ansible.
For this blog post I’m focusing on the Chromecasts we have in our environment. We have both audio and video ChromeCasts, and both provide different capabilities, presented as Items. That makes it a nice example.
Let’s start by getting a list of inbox items:
Now loop over each item in the inbox, and approve every ChromeCast:
The item to approve is specified by the UID, which is part of the listing. Only items which are in the state of
NEW are approved.
loop statement is new in Ansible 2.5. In earlier versions, one of the
with_* methods must be used.
Each ChromeCast provides a number capabilities, and by default only 2 are linked:
volume. For my purpose. I need a few more links, and I’m going to add them automatically. For that I need two kind of information: a list of available things, and a link for adding new links.
o2_links.url provides the URL which is needed to add new links.
o2_things.json provides a list of available Things, we are interested in all the
Unfortunately I was not able to get this working using the new
loop statement, and had to fall back to
with_subelements. This Play uses the
o2_things.json, and loops over all the
channels in there. Then it creates a
new link url and uses
PUT to send this to openHAB - but only if it’s a ChromeCast thing, and only if it’s a channel of a certain wanted type.
Now I have all the channels I’m interested in available in openHAB. I could as well just link all channels, there’s not much left in the list.