Auto-approve and link certain inbox items in openHAB using Ansible
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:
- name: Get inbox
uri:
url: "http://{{ ansible_host }}:8080/rest/inbox"
register: oh2_inbox
changed_when: false
#- name: List inbox
# debug:
# msg: "{{ oh2_inbox }}"
Now loop over each item in the inbox, and approve every Chromecast:
- name: Auto-approve certain inbox items
uri:
url: "{{ oh2_inbox.url }}/{{ item.thingUID }}/approve"
method: POST
when: item.flag == "NEW" and
(item.thingTypeUID == "chromecast:audio" or
item.thingTypeUID == "chromecast:chromecast")
loop: "{{ oh2_inbox.json }}"
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.
The "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: "control" and "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.
- name: Get things
uri:
url: "http://{{ ansible_host }}:8080/rest/things"
register: o2_things
changed_when: false
- name: Get links
uri:
url: "http://{{ ansible_host }}:8080/rest/links"
register: o2_links
changed_when: false
"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 "channels" subcategories.
- name: Link Chromecast items to channels
uri:
url: "{{ o2_links.url }}/{{ item.1.uid | regex_replace(':', '_') }}/{{ item.1.uid | urlencode }}"
method: PUT
when: (item.0.thingTypeUID == "chromecast:chromecast" or item.0.thingTypeUID == "chromecast:audio") and item.1.linkedItems | length == 0 and
(item.1.channelTypeUID == "chromecast:mute" or
item.1.channelTypeUID == "chromecast:idling" or
item.1.channelTypeUID == "chromecast:playuri" or
item.1.channelTypeUID == "chromecast:appName" or
item.1.channelTypeUID == "chromecast:appId" or
item.1.channelTypeUID == "chromecast:currentTime" or
item.1.channelTypeUID == "chromecast:duration" or
item.1.channelTypeUID == "chromecast:albumName" or
item.1.channelTypeUID == "chromecast:artist" or
item.1.channelTypeUID == "chromecast:image" or
item.1.channelTypeUID == "chromecast:title")
with_subelements:
- "{{ o2_things.json }}"
- channels
loop_control:
label: "{{ item.0.thingTypeUID }} - {{ item.1.channelTypeUID }} - {{ item.0.UID }}"
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.
Comments
Display comments as Linear | Threaded