Migrate LCN configuration to ConfigEntry (Part 1) (#44090)

This commit is contained in:
Andre Lengwenus 2021-03-15 13:45:13 +01:00 committed by GitHub
parent fb34d1a56e
commit 50b7b1cc7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 885 additions and 350 deletions

View file

@ -1,68 +1,76 @@
"""Support for LCN climate control."""
import pypck
from homeassistant.components.climate import ClimateEntity, const
from homeassistant.components.climate import (
DOMAIN as DOMAIN_CLIMATE,
ClimateEntity,
const,
)
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_ADDRESS,
CONF_DOMAIN,
CONF_ENTITIES,
CONF_SOURCE,
CONF_UNIT_OF_MEASUREMENT,
)
from . import LcnEntity
from .const import (
CONF_CONNECTIONS,
CONF_DOMAIN_DATA,
CONF_LOCKABLE,
CONF_MAX_TEMP,
CONF_MIN_TEMP,
CONF_SETPOINT,
DATA_LCN,
)
from .helpers import get_connection
from .helpers import get_device_connection
PARALLEL_UPDATES = 0
async def async_setup_platform(
hass, hass_config, async_add_entities, discovery_info=None
):
"""Set up the LCN climate platform."""
if discovery_info is None:
return
def create_lcn_climate_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)
return LcnClimate(entity_config, config_entry.entry_id, device_connection)
devices.append(LcnClimate(config, address_connection))
async_add_entities(devices)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up LCN switch entities from a config entry."""
entities = []
for entity_config in config_entry.data[CONF_ENTITIES]:
if entity_config[CONF_DOMAIN] == DOMAIN_CLIMATE:
entities.append(
create_lcn_climate_entity(hass, entity_config, config_entry)
)
async_add_entities(entities)
class LcnClimate(LcnEntity, ClimateEntity):
"""Representation of a LCN climate device."""
def __init__(self, config, device_connection):
def __init__(self, config, entry_id, device_connection):
"""Initialize of a LCN climate device."""
super().__init__(config, device_connection)
super().__init__(config, entry_id, device_connection)
self.variable = pypck.lcn_defs.Var[config[CONF_SOURCE]]
self.setpoint = pypck.lcn_defs.Var[config[CONF_SETPOINT]]
self.unit = pypck.lcn_defs.VarUnit.parse(config[CONF_UNIT_OF_MEASUREMENT])
self.variable = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
self.setpoint = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SETPOINT]]
self.unit = pypck.lcn_defs.VarUnit.parse(
config[CONF_DOMAIN_DATA][CONF_UNIT_OF_MEASUREMENT]
)
self.regulator_id = pypck.lcn_defs.Var.to_set_point_id(self.setpoint)
self.is_lockable = config[CONF_LOCKABLE]
self._max_temp = config[CONF_MAX_TEMP]
self._min_temp = config[CONF_MIN_TEMP]
self.is_lockable = config[CONF_DOMAIN_DATA][CONF_LOCKABLE]
self._max_temp = config[CONF_DOMAIN_DATA][CONF_MAX_TEMP]
self._min_temp = config[CONF_DOMAIN_DATA][CONF_MIN_TEMP]
self._current_temperature = None
self._target_temperature = None
self._is_on = None
self._is_on = True
async def async_added_to_hass(self):
"""Run when entity about to be added to hass."""
@ -71,6 +79,13 @@ class LcnClimate(LcnEntity, ClimateEntity):
await self.device_connection.activate_status_request_handler(self.variable)
await self.device_connection.activate_status_request_handler(self.setpoint)
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.variable)
await self.device_connection.cancel_status_request_handler(self.setpoint)
@property
def supported_features(self):
"""Return the list of supported features."""