-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_sensor.py
More file actions
111 lines (91 loc) · 3.4 KB
/
binary_sensor.py
File metadata and controls
111 lines (91 loc) · 3.4 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import logging
from .channel import Channel
from .const import DOMAIN, SCANNER
from .vscp.const import (CLASS_INFORMATION, EVENT_INFORMATION_ON, EVENT_INFORMATION_OFF)
from .vscp.util import read_reg
from homeassistant.components.binary_sensor import (BinarySensorEntity)
from homeassistant.const import STATE_OFF, STATE_ON
_LOGGER = logging.getLogger(__name__)
IDENTIFIER = 'BS'
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
if discovery_info is None:
#to do, add entries from configuration.yaml here
return
else:
for node in hass.data[DOMAIN][SCANNER].nodes.values():
async_add_entities([ch for ch in node.get_channels(IDENTIFIER) if ch.enabled])
return True
class vscpBinarySensor(BinarySensorEntity, Channel):
"""Representation of an VSCP binary sensor."""
@classmethod
async def new(cls, node, channel):
self = cls()
self._node = node
self._channel = channel
registers = await read_reg(node.bus, node.nickname, channel, 0, 34)
self._enabled = (registers[0x03] != 0x00)
self._state = (registers[0x04] != 0x00)
self._class_id = int(registers[0x05])
self._name = registers[16:33].decode().rstrip('/x0')
self.entity_id = "binary_sensor.vscp.{}.{}".format(self._node.guid, self._channel)
return self
async def async_added_to_hass(self):
await super().async_added_to_hass()
await self._node.updater.sub_ch_event(self._node.nickname, self._channel, CLASS_INFORMATION, EVENT_INFORMATION_ON, self._handle_onoff_event)
await self._node.updater.sub_ch_event(self._node.nickname, self._channel, CLASS_INFORMATION, EVENT_INFORMATION_OFF, self._handle_onoff_event)
@property
def is_on(self):
return self._state
@property
def state(self):
"""Return the state of the sensor."""
return STATE_ON if self._state else STATE_OFF
@classmethod
def identifier(cls):
return IDENTIFIER
@property
def name(self):
"""Return the display name of this light."""
return self._name
# when a node goes offline, we can disable it's channels
@property
def available(self):
return True
@property
def unique_id(self):
return "BS-{}-{}".format(self._node.guid, self._channel)
@property
def should_poll(self):
return False
async def _handle_onoff_event(self, event):
self._state = (event.vscp_type == EVENT_INFORMATION_ON)
self.async_schedule_update_ha_state()
@property
def device_class(self):
class_map = {
0x01 : 'battery',
0x02 : 'battery_charging',
0x03 : 'cold',
0x04 : 'connectivity',
0x05 : 'door',
0x06 : 'garage_door',
0x07 : 'gas',
0x08 : 'heat',
0x08 : 'light',
0x09 : 'lock',
0x0A : 'moisture',
0x0B : 'motion',
0x0C : 'moving',
0x0D : 'occupancy',
0x0E : 'opening',
0x0F : 'plug',
0x10 : 'power',
0x11 : 'presence',
0x12 : 'problem',
0x13 : 'safety',
0x14 : 'smoke',
0x15 : 'sound',
0x16 : 'vibration',
0x17 : 'window'
}
return class_map[self._class_id] if self._class_id in class_map else 'generic'