-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp_board_binary_sensor.py
More file actions
45 lines (35 loc) · 1.49 KB
/
esp_board_binary_sensor.py
File metadata and controls
45 lines (35 loc) · 1.49 KB
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
39
40
41
42
43
44
45
from homeassistant import loader
from homeassistant.components.binary_sensor.mqtt import MqttBinarySensor
from itertools import chain
import json
DEPENDENCIES = ['mqtt']
def with_family(family):
def set_family(device):
device['family'] = family
return device
return set_family
def read_only(switch):
return switch['access'] == 'readonly'
def setup_platform(hass, config, add_devices, discovery_info=None):
registered_boards = {}
def connected(topic, payload, qos):
serial = topic[0:topic.find('/')]
if serial in registered_boards:
if registered_boards[serial] != payload:
hass.services.call('homeassistant', 'restart')
else:
board_config = json.loads(payload)
devices = chain(map(with_family('switches'), filter(read_only, board_config['switches'])),
map(with_family('sensors'), board_config['sensors']))
add_devices(MqttBinarySensor(
hass=hass,
name=device['name'],
state_topic='{}/{}/{}'.format(serial, device['family'], device['topic']),
sensor_class=device['type'] if device['type'] != 'generic' else None,
qos=0,
payload_on='ON',
payload_off='OFF',
value_template=None
) for device in devices)
registered_boards[serial] = payload
loader.get_component('mqtt').subscribe(hass, '+/config', connected)