-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49b92b5
commit d3f73c6
Showing
3 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
"""Support for LCN binary sensors.""" | ||
from homeassistant.components.binary_sensor import BinarySensorDevice | ||
from homeassistant.const import CONF_ADDRESS | ||
|
||
from . import LcnDevice, get_connection | ||
from .const import ( | ||
BINSENSOR_PORTS, CONF_CONNECTIONS, CONF_SOURCE, DATA_LCN, SETPOINTS) | ||
|
||
DEPENDENCIES = ['lcn'] | ||
|
||
|
||
async def async_setup_platform(hass, hass_config, async_add_entities, | ||
discovery_info=None): | ||
"""Set up the LCN binary sensor platform.""" | ||
if discovery_info is None: | ||
return | ||
|
||
import pypck | ||
|
||
devices = [] | ||
for config in discovery_info: | ||
address, connection_id = config[CONF_ADDRESS] | ||
addr = pypck.lcn_addr.LcnAddr(*address) | ||
connections = hass.data[DATA_LCN][CONF_CONNECTIONS] | ||
connection = get_connection(connections, connection_id) | ||
address_connection = connection.get_address_conn(addr) | ||
|
||
if config[CONF_SOURCE] in SETPOINTS: | ||
device = LcnRegulatorLockSensor(config, address_connection) | ||
elif config[CONF_SOURCE] in BINSENSOR_PORTS: | ||
device = LcnBinarySensor(config, address_connection) | ||
else: # in KEYS | ||
device = LcnLockKeysSensor(config, address_connection) | ||
|
||
devices.append(device) | ||
|
||
async_add_entities(devices) | ||
|
||
|
||
class LcnRegulatorLockSensor(LcnDevice, BinarySensorDevice): | ||
"""Representation of a LCN binary sensor for regulator locks.""" | ||
|
||
def __init__(self, config, address_connection): | ||
"""Initialize the LCN binary sensor.""" | ||
super().__init__(config, address_connection) | ||
|
||
self.setpoint_variable = \ | ||
self.pypck.lcn_defs.Var[config[CONF_SOURCE]] | ||
|
||
self._value = None | ||
|
||
async def async_added_to_hass(self): | ||
"""Run when entity about to be added to hass.""" | ||
await super().async_added_to_hass() | ||
self.hass.async_create_task( | ||
self.address_connection.activate_status_request_handler( | ||
self.setpoint_variable)) | ||
|
||
@property | ||
def is_on(self): | ||
"""Return true if the binary sensor is on.""" | ||
return self._value | ||
|
||
def input_received(self, input_obj): | ||
"""Set sensor value when LCN input object (command) is received.""" | ||
if not isinstance(input_obj, self.pypck.inputs.ModStatusVar) or \ | ||
input_obj.get_var() != self.setpoint_variable: | ||
return | ||
|
||
self._value = input_obj.get_value().is_locked_regulator() | ||
self.async_schedule_update_ha_state() | ||
|
||
|
||
class LcnBinarySensor(LcnDevice, BinarySensorDevice): | ||
"""Representation of a LCN binary sensor for binary sensor ports.""" | ||
|
||
def __init__(self, config, address_connection): | ||
"""Initialize the LCN binary sensor.""" | ||
super().__init__(config, address_connection) | ||
|
||
self.bin_sensor_port = \ | ||
self.pypck.lcn_defs.BinSensorPort[config[CONF_SOURCE]] | ||
|
||
self._value = None | ||
|
||
async def async_added_to_hass(self): | ||
"""Run when entity about to be added to hass.""" | ||
await super().async_added_to_hass() | ||
self.hass.async_create_task( | ||
self.address_connection.activate_status_request_handler( | ||
self.bin_sensor_port)) | ||
|
||
@property | ||
def is_on(self): | ||
"""Return true if the binary sensor is on.""" | ||
return self._value | ||
|
||
def input_received(self, input_obj): | ||
"""Set sensor value when LCN input object (command) is received.""" | ||
if not isinstance(input_obj, self.pypck.inputs.ModStatusBinSensors): | ||
return | ||
|
||
self._value = input_obj.get_state(self.bin_sensor_port.value) | ||
self.async_schedule_update_ha_state() | ||
|
||
|
||
class LcnLockKeysSensor(LcnDevice, BinarySensorDevice): | ||
"""Representation of a LCN sensor for key locks.""" | ||
|
||
def __init__(self, config, address_connection): | ||
"""Initialize the LCN sensor.""" | ||
super().__init__(config, address_connection) | ||
|
||
self.source = self.pypck.lcn_defs.Key[config[CONF_SOURCE]] | ||
self._value = None | ||
|
||
async def async_added_to_hass(self): | ||
"""Run when entity about to be added to hass.""" | ||
await super().async_added_to_hass() | ||
self.hass.async_create_task( | ||
self.address_connection.activate_status_request_handler( | ||
self.source)) | ||
|
||
@property | ||
def is_on(self): | ||
"""Return true if the binary sensor is on.""" | ||
return self._value | ||
|
||
def input_received(self, input_obj): | ||
"""Set sensor value when LCN input object (command) is received.""" | ||
if not isinstance(input_obj, self.pypck.inputs.ModStatusKeyLocks) or \ | ||
self.source not in self.pypck.lcn_defs.Key: | ||
return | ||
|
||
table_id = ord(self.source.name[0]) - 65 | ||
key_id = int(self.source.name[1]) - 1 | ||
|
||
self._value = input_obj.get_state(table_id, key_id) | ||
self.async_schedule_update_ha_state() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters