Migrate LCN configuration to ConfigEntry (Part 1) (#44090)
This commit is contained in:
parent
fb34d1a56e
commit
50b7b1cc7a
17 changed files with 885 additions and 350 deletions
|
@ -1,49 +1,56 @@
|
|||
"""Support for LCN binary sensors."""
|
||||
import pypck
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.const import CONF_ADDRESS, CONF_SOURCE
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DOMAIN as DOMAIN_BINARY_SENSOR,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_ENTITIES, CONF_SOURCE
|
||||
|
||||
from . import LcnEntity
|
||||
from .const import BINSENSOR_PORTS, CONF_CONNECTIONS, DATA_LCN, SETPOINTS
|
||||
from .helpers import get_connection
|
||||
from .const import BINSENSOR_PORTS, CONF_DOMAIN_DATA, SETPOINTS
|
||||
from .helpers import get_device_connection
|
||||
|
||||
|
||||
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
|
||||
def create_lcn_binary_sensor_entity(hass, entity_config, config_entry):
|
||||
"""Set up an entity for this domain."""
|
||||
device_connection = get_device_connection(
|
||||
hass, tuple(entity_config[CONF_ADDRESS]), config_entry
|
||||
)
|
||||
|
||||
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 entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS:
|
||||
return LcnRegulatorLockSensor(
|
||||
entity_config, config_entry.entry_id, device_connection
|
||||
)
|
||||
if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS:
|
||||
return LcnBinarySensor(entity_config, config_entry.entry_id, device_connection)
|
||||
# in KEY
|
||||
return LcnLockKeysSensor(entity_config, config_entry.entry_id, device_connection)
|
||||
|
||||
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 def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up LCN switch entities from a config entry."""
|
||||
entities = []
|
||||
|
||||
async_add_entities(devices)
|
||||
for entity_config in config_entry.data[CONF_ENTITIES]:
|
||||
if entity_config[CONF_DOMAIN] == DOMAIN_BINARY_SENSOR:
|
||||
entities.append(
|
||||
create_lcn_binary_sensor_entity(hass, entity_config, config_entry)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity):
|
||||
"""Representation of a LCN binary sensor for regulator locks."""
|
||||
|
||||
def __init__(self, config, device_connection):
|
||||
def __init__(self, config, entry_id, device_connection):
|
||||
"""Initialize the LCN binary sensor."""
|
||||
super().__init__(config, device_connection)
|
||||
super().__init__(config, entry_id, device_connection)
|
||||
|
||||
self.setpoint_variable = pypck.lcn_defs.Var[config[CONF_SOURCE]]
|
||||
self.setpoint_variable = pypck.lcn_defs.Var[
|
||||
config[CONF_DOMAIN_DATA][CONF_SOURCE]
|
||||
]
|
||||
|
||||
self._value = None
|
||||
|
||||
|
@ -55,6 +62,14 @@ class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity):
|
|||
self.setpoint_variable
|
||||
)
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Run when entity will be removed from hass."""
|
||||
await super().async_will_remove_from_hass()
|
||||
if not self.device_connection.is_group:
|
||||
await self.device_connection.cancel_status_request_handler(
|
||||
self.setpoint_variable
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
|
@ -75,11 +90,13 @@ class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity):
|
|||
class LcnBinarySensor(LcnEntity, BinarySensorEntity):
|
||||
"""Representation of a LCN binary sensor for binary sensor ports."""
|
||||
|
||||
def __init__(self, config, device_connection):
|
||||
def __init__(self, config, entry_id, device_connection):
|
||||
"""Initialize the LCN binary sensor."""
|
||||
super().__init__(config, device_connection)
|
||||
super().__init__(config, entry_id, device_connection)
|
||||
|
||||
self.bin_sensor_port = pypck.lcn_defs.BinSensorPort[config[CONF_SOURCE]]
|
||||
self.bin_sensor_port = pypck.lcn_defs.BinSensorPort[
|
||||
config[CONF_DOMAIN_DATA][CONF_SOURCE]
|
||||
]
|
||||
|
||||
self._value = None
|
||||
|
||||
|
@ -91,6 +108,14 @@ class LcnBinarySensor(LcnEntity, BinarySensorEntity):
|
|||
self.bin_sensor_port
|
||||
)
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Run when entity will be removed from hass."""
|
||||
await super().async_will_remove_from_hass()
|
||||
if not self.device_connection.is_group:
|
||||
await self.device_connection.cancel_status_request_handler(
|
||||
self.bin_sensor_port
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
|
@ -108,11 +133,11 @@ class LcnBinarySensor(LcnEntity, BinarySensorEntity):
|
|||
class LcnLockKeysSensor(LcnEntity, BinarySensorEntity):
|
||||
"""Representation of a LCN sensor for key locks."""
|
||||
|
||||
def __init__(self, config, device_connection):
|
||||
def __init__(self, config, entry_id, device_connection):
|
||||
"""Initialize the LCN sensor."""
|
||||
super().__init__(config, device_connection)
|
||||
super().__init__(config, entry_id, device_connection)
|
||||
|
||||
self.source = pypck.lcn_defs.Key[config[CONF_SOURCE]]
|
||||
self.source = pypck.lcn_defs.Key[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
|
||||
self._value = None
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
|
@ -121,6 +146,12 @@ class LcnLockKeysSensor(LcnEntity, BinarySensorEntity):
|
|||
if not self.device_connection.is_group:
|
||||
await self.device_connection.activate_status_request_handler(self.source)
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Run when entity will be removed from hass."""
|
||||
await super().async_will_remove_from_hass()
|
||||
if not self.device_connection.is_group:
|
||||
await self.device_connection.cancel_status_request_handler(self.source)
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue