Skip to content

Commit 42bce73

Browse files
committed
Add LCN binary_sensor component
1 parent 49b92b5 commit 42bce73

File tree

3 files changed

+164
-10
lines changed

3 files changed

+164
-10
lines changed

homeassistant/components/lcn/__init__.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
import voluptuous as vol
55

6+
from homeassistant.components.lcn.const import (
7+
BINSENSOR_PORTS, CONF_CONNECTIONS, CONF_DIM_MODE, CONF_DIMMABLE,
8+
CONF_MOTOR, CONF_OUTPUT, CONF_SK_NUM_TRIES, CONF_SOURCE, CONF_TRANSITION,
9+
DATA_LCN, DEFAULT_NAME, DIM_MODES, DOMAIN, KEYS, LED_PORTS, LOGICOP_PORTS,
10+
MOTOR_PORTS, OUTPUT_PORTS, PATTERN_ADDRESS, RELAY_PORTS, S0_INPUTS,
11+
SETPOINTS, THRESHOLDS, VAR_UNITS, VARIABLES)
612
from homeassistant.const import (
7-
CONF_ADDRESS, CONF_COVERS, CONF_HOST, CONF_LIGHTS, CONF_NAME,
8-
CONF_PASSWORD, CONF_PORT, CONF_SENSORS, CONF_SWITCHES,
13+
CONF_ADDRESS, CONF_BINARY_SENSORS, CONF_COVERS, CONF_HOST, CONF_LIGHTS,
14+
CONF_NAME, CONF_PASSWORD, CONF_PORT, CONF_SENSORS, CONF_SWITCHES,
915
CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME)
1016
import homeassistant.helpers.config_validation as cv
1117
from homeassistant.helpers.discovery import async_load_platform
1218
from homeassistant.helpers.entity import Entity
1319

14-
from .const import (
15-
CONF_CONNECTIONS, CONF_DIM_MODE, CONF_DIMMABLE, CONF_MOTOR, CONF_OUTPUT,
16-
CONF_SK_NUM_TRIES, CONF_SOURCE, CONF_TRANSITION, DATA_LCN, DEFAULT_NAME,
17-
DIM_MODES, DOMAIN, LED_PORTS, LOGICOP_PORTS, MOTOR_PORTS, OUTPUT_PORTS,
18-
PATTERN_ADDRESS, RELAY_PORTS, S0_INPUTS, SETPOINTS, THRESHOLDS, VAR_UNITS,
19-
VARIABLES)
20-
2120
_LOGGER = logging.getLogger(__name__)
2221

2322
REQUIREMENTS = ['pypck==0.5.9']
@@ -65,6 +64,13 @@ def is_address(value):
6564
raise vol.error.Invalid('Not a valid address string.')
6665

6766

67+
BINARY_SENSORS_SCHEMA = vol.Schema({
68+
vol.Required(CONF_NAME): cv.string,
69+
vol.Required(CONF_ADDRESS): is_address,
70+
vol.Required(CONF_SOURCE): vol.All(vol.Upper, vol.In(SETPOINTS + KEYS +
71+
BINSENSOR_PORTS))
72+
})
73+
6874
COVERS_SCHEMA = vol.Schema({
6975
vol.Required(CONF_NAME): cv.string,
7076
vol.Required(CONF_ADDRESS): is_address,
@@ -115,6 +121,8 @@ def is_address(value):
115121
DOMAIN: vol.Schema({
116122
vol.Required(CONF_CONNECTIONS): vol.All(
117123
cv.ensure_list, has_unique_connection_names, [CONNECTION_SCHEMA]),
124+
vol.Optional(CONF_BINARY_SENSORS): vol.All(
125+
cv.ensure_list, [BINARY_SENSORS_SCHEMA]),
118126
vol.Optional(CONF_COVERS): vol.All(
119127
cv.ensure_list, [COVERS_SCHEMA]),
120128
vol.Optional(CONF_LIGHTS): vol.All(
@@ -177,7 +185,8 @@ async def async_setup(hass, config):
177185
hass.data[DATA_LCN][CONF_CONNECTIONS] = connections
178186

179187
# load platforms
180-
for component, conf_key in (('cover', CONF_COVERS),
188+
for component, conf_key in (('binary_sensor', CONF_BINARY_SENSORS),
189+
('cover', CONF_COVERS),
181190
('light', CONF_LIGHTS),
182191
('sensor', CONF_SENSORS),
183192
('switch', CONF_SWITCHES)):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""Support for LCN binary sensors."""
2+
from homeassistant.components.binary_sensor import BinarySensorDevice
3+
from homeassistant.components.lcn import LcnDevice, get_connection
4+
from homeassistant.components.lcn.const import (
5+
BINSENSOR_PORTS, CONF_CONNECTIONS, CONF_SOURCE, DATA_LCN, SETPOINTS)
6+
from homeassistant.const import CONF_ADDRESS
7+
8+
DEPENDENCIES = ['lcn']
9+
10+
11+
async def async_setup_platform(hass, hass_config, async_add_entities,
12+
discovery_info=None):
13+
"""Set up the LCN binary sensor platform."""
14+
if discovery_info is None:
15+
return
16+
17+
import pypck
18+
19+
devices = []
20+
for config in discovery_info:
21+
address, connection_id = config[CONF_ADDRESS]
22+
addr = pypck.lcn_addr.LcnAddr(*address)
23+
connections = hass.data[DATA_LCN][CONF_CONNECTIONS]
24+
connection = get_connection(connections, connection_id)
25+
address_connection = connection.get_address_conn(addr)
26+
27+
if config[CONF_SOURCE] in SETPOINTS:
28+
device = LcnRegulatorLockSensor(config, address_connection)
29+
elif config[CONF_SOURCE] in BINSENSOR_PORTS:
30+
device = LcnBinarySensor(config, address_connection)
31+
else: # in KEYS
32+
device = LcnLockKeysSensor(config, address_connection)
33+
34+
devices.append(device)
35+
36+
async_add_entities(devices)
37+
38+
39+
class LcnRegulatorLockSensor(LcnDevice, BinarySensorDevice):
40+
"""Representation of a LCN binary sensor for regulator locks."""
41+
42+
def __init__(self, config, address_connection):
43+
"""Initialize the LCN binary sensor."""
44+
super().__init__(config, address_connection)
45+
46+
self.setpoint_variable = \
47+
self.pypck.lcn_defs.Var[config[CONF_SOURCE]]
48+
49+
self._value = None
50+
51+
async def async_added_to_hass(self):
52+
"""Run when entity about to be added to hass."""
53+
await super().async_added_to_hass()
54+
self.hass.async_create_task(
55+
self.address_connection.activate_status_request_handler(
56+
self.setpoint_variable))
57+
58+
@property
59+
def is_on(self):
60+
"""Return true if the binary sensor is on."""
61+
return self._value
62+
63+
def input_received(self, input_obj):
64+
"""Set sensor value when LCN input object (command) is received."""
65+
if not isinstance(input_obj, self.pypck.inputs.ModStatusVar) or \
66+
input_obj.get_var() != self.setpoint_variable:
67+
return
68+
69+
self._value = input_obj.get_value().is_locked_regulator()
70+
self.async_schedule_update_ha_state()
71+
72+
73+
class LcnBinarySensor(LcnDevice, BinarySensorDevice):
74+
"""Representation of a LCN binary sensor for binary sensor ports."""
75+
76+
def __init__(self, config, address_connection):
77+
"""Initialize the LCN binary sensor."""
78+
super().__init__(config, address_connection)
79+
80+
self.bin_sensor_port = \
81+
self.pypck.lcn_defs.BinSensorPort[config[CONF_SOURCE]]
82+
83+
self._value = None
84+
85+
async def async_added_to_hass(self):
86+
"""Run when entity about to be added to hass."""
87+
await super().async_added_to_hass()
88+
self.hass.async_create_task(
89+
self.address_connection.activate_status_request_handler(
90+
self.bin_sensor_port))
91+
92+
@property
93+
def is_on(self):
94+
"""Return true if the binary sensor is on."""
95+
return self._value
96+
97+
def input_received(self, input_obj):
98+
"""Set sensor value when LCN input object (command) is received."""
99+
if not isinstance(input_obj, self.pypck.inputs.ModStatusBinSensors):
100+
return
101+
102+
self._value = input_obj.get_state(self.bin_sensor_port.value)
103+
self.async_schedule_update_ha_state()
104+
105+
106+
class LcnLockKeysSensor(LcnDevice, BinarySensorDevice):
107+
"""Representation of a LCN sensor for key locks."""
108+
109+
def __init__(self, config, address_connection):
110+
"""Initialize the LCN sensor."""
111+
super().__init__(config, address_connection)
112+
113+
self.source = self.pypck.lcn_defs.Key[config[CONF_SOURCE]]
114+
self._value = None
115+
116+
async def async_added_to_hass(self):
117+
"""Run when entity about to be added to hass."""
118+
await super().async_added_to_hass()
119+
self.hass.async_create_task(
120+
self.address_connection.activate_status_request_handler(
121+
self.source))
122+
123+
@property
124+
def is_on(self):
125+
"""Return true if the binary sensor is on."""
126+
return self._value
127+
128+
def input_received(self, input_obj):
129+
"""Set sensor value when LCN input object (command) is received."""
130+
if not isinstance(input_obj, self.pypck.inputs.ModStatusKeyLocks) or \
131+
self.source not in self.pypck.lcn_defs.Key:
132+
return
133+
134+
table_id = ord(self.source.name[0]) - 65
135+
key_id = int(self.source.name[1]) - 1
136+
137+
self._value = input_obj.get_state(table_id, key_id)
138+
self.async_schedule_update_ha_state()

homeassistant/components/lcn/const.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding: utf-8
22
"""Constants for the LCN component."""
3+
from itertools import product
34
import re
45

56
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
@@ -37,6 +38,12 @@
3738

3839
LOGICOP_PORTS = ['LOGICOP1', 'LOGICOP2', 'LOGICOP3', 'LOGICOP4']
3940

41+
BINSENSOR_PORTS = ['BINSENSOR1', 'BINSENSOR2', 'BINSENSOR3', 'BINSENSOR4',
42+
'BINSENSOR5', 'BINSENSOR6', 'BINSENSOR7', 'BINSENSOR8']
43+
44+
KEYS = ['{:s}{:d}'.format(t[0], t[1]) for t in product(['A', 'B', 'C', 'D'],
45+
range(1, 9))]
46+
4047
VARIABLES = ['VAR1ORTVAR', 'VAR2ORR1VAR', 'VAR3ORR2VAR',
4148
'TVAR', 'R1VAR', 'R2VAR',
4249
'VAR1', 'VAR2', 'VAR3', 'VAR4', 'VAR5', 'VAR6',

0 commit comments

Comments
 (0)