Install openHAB2 bindings using Ansible

Posted by ads' corner on Sunday, 2018-07-01
Posted in [Ansible][Linux][Openhab][Raspberry-Pi]

Recently I started looking into Home Automation, using a Raspberry Pi and the openHAB as platform. The website provides ready-to-go images based on Debian, named openHABian. Me being me, I decided to install the image, let it boot up for the first time, and then take it from there using Ansible as automation tool. That’s a bit more effort in the beginning, but ensures that I can always just wipe the SDcard, and start over from the beginning. Especially useful if I screw up at some point during testing.

One of the first steps to do is to decide which bindings, or extensions, are needed. Bindings provide openHAB with information about what kind of hardware and input sources are available out there. The list of available bindings is long, and one can use the UI to click and install bindings, before starting to configure and link them. That process is not idempotent, but luckily openHAB also provides an API which can be used to execute the same steps, just faster and automated.

The following Playbook uses the API to receive a list of available extensions/bindings, and then installs a number of extensions if they are not yet installed. Additionally a new filter byattr filter is required, based on this solution here. In my case I did not use filter_plugins/core.py but instead placed the plugin in filter_plugins/byattr.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- name: Install required packages
  apt: name={{ item }} state=present
  with_items:
    - python-jinja2
    - python-jinja2-time
    - jq
    - curl

- name: Get list of available and installed extensions
  uri:
    url: "http://{{ ansible_host }}:8080/rest/extensions"
  register: oh2_extensions
  changed_when: false

#- name: List extensions
#  debug:
#    msg: "{{ oh2_extensions.json }}"

#- name: List extensions
#  debug:
#    msg: "{{ item.id }}"
#  loop: "{{ oh2_extensions.json }}"

- name: Install extensions
  uri:
    url: "http://{{ ansible_host }}:8080/rest/extensions/{{ item }}/install"
    method: POST
  when: "not (oh2_extensions.json|byattr('id', item))[0].installed"
  with_items:
    - binding-chromecast
    - binding-logreader
    - binding-network
    - binding-yahooweather
    - binding-ipp
    - binding-feed
    - binding-exec
    - binding-astro
  register: oh2_install_extensions

The first debug statement dumps the entire extension list including all the other data. The second debug just dumps a list of available extensions, making it somewhat easier to read. Both debug options are commented out, but left there for convenience.

This Playbook installs a few additional packages, mainly jinja2 templates which are not preinstalled on the openHABian image, and a small tool named jq which I’m not directly using with Ansible, but in combination with curl to debug the API JSON output:

1
curl -s -X GET --header "Accept: application/json" "http://127.0.0.1:8080/rest/things" | jq '.' | less

The curl line above fetches all available things from openHAB, pretty prints the output and lists everything in less.

Now that all extensions are installed, it’s time to rescan and rediscover new inbox items. Again that’s possible using the API:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
- block:
  - name: Get list of discoveries
    uri:
      url: "http://{{ ansible_host }}:8080/rest/discovery"
    register: oh2_discoveries
    changed_when: false

#  - name: List discoveries
#    debug:
#      msg: "{{ oh2_discoveries }}"

  - name: Rediscover
    uri:
      url: "{{ oh2_discoveries.url }}/bindings/{{ item }}/scan"
      method: POST
    loop: "{{ oh2_discoveries.json }}"

  when: oh2_install_extensions.changed

Fetch a list of all available discoveries, and then force openHAB to rediscover all of them - just in case. That’s all wrapped in a block, and only run if the list of extensions changed. Otherwise it’s not necessary to rediscover items.


Categories: [Ansible] [Linux] [Openhab] [Raspberry-Pi]