|
| 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() |
0 commit comments