Add a FRITZ!Box to openHAB, using Ansible

Posted by ads' corner on Wednesday, 2018-07-04
Posted in [Ansible][Linux][Openhab]

Next step on my way to add home automation: the FRITZ!Box. Mostly for the current IP-address, and call information.

After some research it became obvious that more manual work is required, hence again something which can be automated.

openHAB has 3 different modules available which support the AVM FRITZ!Box:

  • AVM FRITZ! Binding: This only supports the home automation on the FRITZ!Box, no information about calls or the box itself is available. Not sure if that will be added to this binding at some point. That is also the only extension which is native for 2.0.
  • Fritzbox Binding (using TR064 protocol): This extension provides call and status information from the FRITZ!Box itself. It’s a legacy 1.0 extension, and uses the TR064 protocol to fetch the information.
  • Fritz!Box Binding: That’s an old and legacy 1.0 binding which requires telnet access to the FRITZ!Box. Since the telnet support is disabled since FRITZ!OS 6, this extension is more or less useless these days.

For my purpose I need the fritzboxtr0641 binding in order to receive status and call information. Given that it’s a legacy binding, first the legacy bindings need to be activated:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
- name: Update extensions configuration
  lineinfile:
    dest: /etc/openhab2/services/addons.cfg
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
    state: "{{ item.state }}"
    create: yes
  with_items:
    - { regexp: '^#? *package', line: 'package = expert', state: present }
    - { regexp: '^#? *remote', line: 'remote = true', state: present }
    - { regexp: '^#? *legacy', line: 'legacy = true', state: present }

The binding needs the libhttpclient library for Java:

1
2
3
4
5
6
- name: Install required packages
  apt: name={{ item }} state=present
  with_items:
    - openhab2-addons
    - openhab2-addons-legacy
    - libhttpclient-java

The extension install:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
- name: Get list of available and installed extensions
  uri:
    url: "http://{{ ansible_host }}:8080/rest/extensions"
  register: oh2_extensions
  changed_when: false

- 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-fritzboxtr0641
  register: oh2_install_extensions

This extension installs a configuration file, which mainly contains connection information for the FRITZ!Box: IP, username, password. It is strongly recommended to create a separate account on the FRITZ!Box.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
- name: Update fritzboxtr064.cfg
  lineinfile:
    dest: /etc/openhab2/services/fritzboxtr064.cfg
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
    state: "{{ item.state }}"
    create: yes
  with_items:
    - { regexp: '^#? *url', line: 'url=http://{{ fritzbox_ip }}:49000', state: present }
    - { regexp: '^#? *user', line: 'user={{ fritzbox_user }}', state: present }
    - { regexp: '^#? *pass', line: 'pass={{ fritzbox_password }}', state: present }

The call monitor on the FRITZ!Box is disabled by default. Without this monitor enabled, all the callmonitor_* Items will not work. Unfortunately the plugin will not log any useful information, but rather just log a connection refused, a Java stack trace, and will keep trying with an increasing timeout.

To enable the call monitor, call #96*5* from any connected DECT telephone. And #96*4* turns the call monitor off again.

Now it’s time to add the available Items. That must happen in an .items file, because legacy extensions can’t be configured using the web UI. A good place is /etc/openhab2/items/fritzboxtr0641.items:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
String		fboxName			"FBox Model [%s]"		{fritzboxtr064="modelName"}
// get wan ip if FritzBox establishes the internet connection (e. g. via DSL)
String		fboxWanIP			"FBox WAN IP [%s]"		{fritzboxtr064="wanip"}
// get wan ip if FritzBox uses internet connection of external router
//String		fboxWanIPExternal		"FBox external WAN IP [%s]"	{fritzboxtr064="externalWanip"}

// only when using call monitor
// #96*5* - enable callmonitor
// #96*4* - disable callmonitor
Switch		fboxRinging			"Phone ringing [%s]"		{fritzboxtr064="callmonitor_ringing" }
Switch		fboxRinging_Out			"Phone ringing [%s]"		{fritzboxtr064="callmonitor_outgoing" }
Call		fboxIncomingCall		"Incoming call: [%1$s to %2$s]"	{fritzboxtr064="callmonitor_ringing" }
Call		fboxOutgoingCall		"Outgoing call: [%1$s to %2$s]"	{fritzboxtr064="callmonitor_outgoing" }

// resolve numbers to names according phonebook
Call		fboxIncomingCallResolved	"Incoming call: [%1$s to %2$s]"	{fritzboxtr064="callmonitor_ringing:resolveName" }

//// Telephone answering machine (TAM) items
//// Number after tamSwitch is ID of configured TAM, start with 0
Switch		fboxTAM0Switch			"Answering machine ID 0"	{fritzboxtr064="tamSwitch:0"}
Number		fboxTAM0NewMsg			"New Messages TAM 0 [%s]"	{fritzboxtr064="tamNewMessages:0"}

// Missed calls: specify the number of last days which should be searched for missed calls
Number		fboxMissedCalls			"Missed Calls [%s]"		{fritzboxtr064="missedCallsInDays:5"}

Obviously I don’t edit the file on the openHAB server, but locally and copy it to the server:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
- name: Copy items files
  copy:
    src: "{{ playbook_dir }}/openhab/{{ item }}"
    dest: "/etc/openhab2/items/{{ item }}"
    owner: openhab
    group: openhabian
    mode: 0640
  with_items:
    - fritzboxtr0641.items
  register: items_files

Categories: [Ansible] [Linux] [Openhab]