Refactor LcnEntity signature (#124411)

* Refactorings due to change of LcnEntity signature

* Fix PR comments

* Move parent class LcnEntity to entity.py
This commit is contained in:
Andre Lengwenus 2024-09-10 19:38:40 +02:00 committed by GitHub
parent aa8f98392d
commit 2b3a6e5361
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 157 additions and 258 deletions

View File

@ -2,35 +2,27 @@
from __future__ import annotations
from collections.abc import Callable
from functools import partial
import logging
import pypck
from pypck.connection import PchkConnectionManager
from homeassistant import config_entries
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
CONF_ADDRESS,
CONF_DEVICE_ID,
CONF_DOMAIN,
CONF_IP_ADDRESS,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_RESOURCE,
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType
from .const import (
ADD_ENTITIES_CALLBACKS,
CONF_DIM_MODE,
CONF_DOMAIN_DATA,
CONF_SK_NUM_TRIES,
CONNECTION,
DOMAIN,
@ -38,11 +30,9 @@ from .const import (
)
from .helpers import (
AddressType,
DeviceConnectionType,
InputType,
async_update_config_entry,
generate_unique_id,
get_device_model,
import_lcn_config,
register_lcn_address_devices,
register_lcn_host_device,
@ -67,16 +57,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
context={"source": SOURCE_IMPORT},
data=config_entry_data,
)
)
return True
async def async_setup_entry(
hass: HomeAssistant, config_entry: config_entries.ConfigEntry
) -> bool:
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Set up a connection to PCHK host from a config entry."""
hass.data.setdefault(DOMAIN, {})
if config_entry.entry_id in hass.data[DOMAIN]:
@ -149,9 +137,7 @@ async def async_setup_entry(
return True
async def async_unload_entry(
hass: HomeAssistant, config_entry: config_entries.ConfigEntry
) -> bool:
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Close connection to PCHK host represented by config_entry."""
# forward unloading to platforms
unload_ok = await hass.config_entries.async_unload_platforms(
@ -172,7 +158,7 @@ async def async_unload_entry(
def async_host_input_received(
hass: HomeAssistant,
config_entry: config_entries.ConfigEntry,
config_entry: ConfigEntry,
device_registry: dr.DeviceRegistry,
inp: pypck.inputs.Input,
) -> None:
@ -242,75 +228,3 @@ def _async_fire_send_keys_event(
event_data.update({CONF_DEVICE_ID: device.id})
hass.bus.async_fire("lcn_send_keys", event_data)
class LcnEntity(Entity):
"""Parent class for all entities associated with the LCN component."""
_attr_should_poll = False
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN device."""
self.config = config
self.entry_id = entry_id
self.device_connection = device_connection
self._unregister_for_inputs: Callable | None = None
self._name: str = config[CONF_NAME]
@property
def address(self) -> AddressType:
"""Return LCN address."""
return (
self.device_connection.seg_id,
self.device_connection.addr_id,
self.device_connection.is_group,
)
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return generate_unique_id(
self.entry_id, self.address, self.config[CONF_RESOURCE]
)
@property
def device_info(self) -> DeviceInfo | None:
"""Return device specific attributes."""
address = f"{'g' if self.address[2] else 'm'}{self.address[0]:03d}{self.address[1]:03d}"
model = (
"LCN resource"
f" ({get_device_model(self.config[CONF_DOMAIN], self.config[CONF_DOMAIN_DATA])})"
)
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
name=f"{address}.{self.config[CONF_RESOURCE]}",
model=model,
manufacturer="Issendorff",
via_device=(
DOMAIN,
generate_unique_id(self.entry_id, self.config[CONF_ADDRESS]),
),
)
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
if not self.device_connection.is_group:
self._unregister_for_inputs = self.device_connection.register_for_inputs(
self.input_received
)
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if self._unregister_for_inputs is not None:
self._unregister_for_inputs()
@property
def name(self) -> str:
"""Return the name of the device."""
return self._name
def input_received(self, input_obj: InputType) -> None:
"""Set state/value when LCN input object (command) is received."""

View File

@ -10,12 +10,11 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_ENTITIES, CONF_SOURCE
from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES, CONF_SOURCE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import (
ADD_ENTITIES_CALLBACKS,
BINSENSOR_PORTS,
@ -23,11 +22,11 @@ from .const import (
DOMAIN,
SETPOINTS,
)
from .helpers import DeviceConnectionType, InputType, get_device_connection
from .entity import LcnEntity
from .helpers import InputType
def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
@ -35,26 +34,12 @@ def add_lcn_entities(
"""Add entities for this domain."""
entities: list[LcnRegulatorLockSensor | LcnBinarySensor | LcnLockKeysSensor] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS:
entities.append(
LcnRegulatorLockSensor(
entity_config, config_entry.entry_id, device_connection
)
)
entities.append(LcnRegulatorLockSensor(entity_config, config_entry))
elif entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS:
entities.append(
LcnBinarySensor(entity_config, config_entry.entry_id, device_connection)
)
entities.append(LcnBinarySensor(entity_config, config_entry))
else: # in KEY
entities.append(
LcnLockKeysSensor(
entity_config, config_entry.entry_id, device_connection
)
)
entities.append(LcnLockKeysSensor(entity_config, config_entry))
async_add_entities(entities)
@ -67,7 +52,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry."""
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
@ -88,11 +72,9 @@ async def async_setup_entry(
class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity):
"""Representation of a LCN binary sensor for regulator locks."""
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN binary sensor."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.setpoint_variable = pypck.lcn_defs.Var[
config[CONF_DOMAIN_DATA][CONF_SOURCE]
@ -129,11 +111,9 @@ class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity):
class LcnBinarySensor(LcnEntity, BinarySensorEntity):
"""Representation of a LCN binary sensor for binary sensor ports."""
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN binary sensor."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.bin_sensor_port = pypck.lcn_defs.BinSensorPort[
config[CONF_DOMAIN_DATA][CONF_SOURCE]
@ -167,11 +147,9 @@ class LcnBinarySensor(LcnEntity, BinarySensorEntity):
class LcnLockKeysSensor(LcnEntity, BinarySensorEntity):
"""Representation of a LCN sensor for key locks."""
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN sensor."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.source = pypck.lcn_defs.Key[config[CONF_DOMAIN_DATA][CONF_SOURCE]]

View File

@ -15,7 +15,6 @@ from homeassistant.components.climate import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_ADDRESS,
CONF_DOMAIN,
CONF_ENTITIES,
CONF_SOURCE,
@ -26,7 +25,6 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import (
ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA,
@ -36,27 +34,21 @@ from .const import (
CONF_SETPOINT,
DOMAIN,
)
from .helpers import DeviceConnectionType, InputType, get_device_connection
from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0
def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
) -> None:
"""Add entities for this domain."""
entities: list[LcnClimate] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
entities.append(
LcnClimate(entity_config, config_entry.entry_id, device_connection)
)
entities = [
LcnClimate(entity_config, config_entry) for entity_config in entity_configs
]
async_add_entities(entities)
@ -69,7 +61,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry."""
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
@ -92,11 +83,9 @@ class LcnClimate(LcnEntity, ClimateEntity):
_enable_turn_on_off_backwards_compatibility = False
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize of a LCN climate device."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.variable = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
self.setpoint = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SETPOINT]]

View File

@ -8,12 +8,11 @@ import pypck
from homeassistant.components.cover import DOMAIN as DOMAIN_COVER, CoverEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_ENTITIES
from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import (
ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA,
@ -21,13 +20,13 @@ from .const import (
CONF_REVERSE_TIME,
DOMAIN,
)
from .helpers import DeviceConnectionType, InputType, get_device_connection
from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0
def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
@ -35,18 +34,10 @@ def add_lcn_entities(
"""Add entities for this domain."""
entities: list[LcnOutputsCover | LcnRelayCover] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
if entity_config[CONF_DOMAIN_DATA][CONF_MOTOR] in "OUTPUTS":
entities.append(
LcnOutputsCover(entity_config, config_entry.entry_id, device_connection)
)
entities.append(LcnOutputsCover(entity_config, config_entry))
else: # in RELAYS
entities.append(
LcnRelayCover(entity_config, config_entry.entry_id, device_connection)
)
entities.append(LcnRelayCover(entity_config, config_entry))
async_add_entities(entities)
@ -59,7 +50,6 @@ async def async_setup_entry(
"""Set up LCN cover entities from a config entry."""
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
@ -85,11 +75,9 @@ class LcnOutputsCover(LcnEntity, CoverEntity):
_attr_is_opening = False
_attr_assumed_state = True
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN cover."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.output_ids = [
pypck.lcn_defs.OutputPort["OUTPUTUP"].value,
@ -189,11 +177,9 @@ class LcnRelayCover(LcnEntity, CoverEntity):
_attr_is_opening = False
_attr_assumed_state = True
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN cover."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.motor = pypck.lcn_defs.MotorPort[config[CONF_DOMAIN_DATA][CONF_MOTOR]]
self.motor_port_onoff = self.motor.value * 2

View File

@ -0,0 +1,90 @@
"""LCN parent entity class."""
from collections.abc import Callable
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_NAME, CONF_RESOURCE
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType
from .const import CONF_DOMAIN_DATA, DOMAIN
from .helpers import (
AddressType,
DeviceConnectionType,
InputType,
generate_unique_id,
get_device_connection,
get_device_model,
)
class LcnEntity(Entity):
"""Parent class for all entities associated with the LCN component."""
_attr_should_poll = False
device_connection: DeviceConnectionType
def __init__(
self,
config: ConfigType,
config_entry: ConfigEntry,
) -> None:
"""Initialize the LCN device."""
self.config = config
self.config_entry = config_entry
self.address: AddressType = config[CONF_ADDRESS]
self._unregister_for_inputs: Callable | None = None
self._name: str = config[CONF_NAME]
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return generate_unique_id(
self.config_entry.entry_id, self.address, self.config[CONF_RESOURCE]
)
@property
def device_info(self) -> DeviceInfo | None:
"""Return device specific attributes."""
address = f"{'g' if self.address[2] else 'm'}{self.address[0]:03d}{self.address[1]:03d}"
model = (
"LCN resource"
f" ({get_device_model(self.config[CONF_DOMAIN], self.config[CONF_DOMAIN_DATA])})"
)
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
name=f"{address}.{self.config[CONF_RESOURCE]}",
model=model,
manufacturer="Issendorff",
via_device=(
DOMAIN,
generate_unique_id(
self.config_entry.entry_id, self.config[CONF_ADDRESS]
),
),
)
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
self.device_connection = get_device_connection(
self.hass, self.config[CONF_ADDRESS], self.config_entry
)
if not self.device_connection.is_group:
self._unregister_for_inputs = self.device_connection.register_for_inputs(
self.input_received
)
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if self._unregister_for_inputs is not None:
self._unregister_for_inputs()
@property
def name(self) -> str:
"""Return the name of the device."""
return self._name
def input_received(self, input_obj: InputType) -> None:
"""Set state/value when LCN input object (command) is received."""

View File

@ -84,7 +84,7 @@ DOMAIN_LOOKUP = {
def get_device_connection(
hass: HomeAssistant, address: AddressType, config_entry: ConfigEntry
) -> DeviceConnectionType | None:
) -> DeviceConnectionType:
"""Return a lcn device_connection."""
host_connection = hass.data[DOMAIN][config_entry.entry_id][CONNECTION]
addr = pypck.lcn_addr.LcnAddr(*address)

View File

@ -15,12 +15,11 @@ from homeassistant.components.light import (
LightEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_ENTITIES
from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import (
ADD_ENTITIES_CALLBACKS,
CONF_DIMMABLE,
@ -30,13 +29,13 @@ from .const import (
DOMAIN,
OUTPUT_PORTS,
)
from .helpers import DeviceConnectionType, InputType, get_device_connection
from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0
def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
@ -44,18 +43,10 @@ def add_lcn_entities(
"""Add entities for this domain."""
entities: list[LcnOutputLight | LcnRelayLight] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS:
entities.append(
LcnOutputLight(entity_config, config_entry.entry_id, device_connection)
)
entities.append(LcnOutputLight(entity_config, config_entry))
else: # in RELAY_PORTS
entities.append(
LcnRelayLight(entity_config, config_entry.entry_id, device_connection)
)
entities.append(LcnRelayLight(entity_config, config_entry))
async_add_entities(entities)
@ -68,7 +59,6 @@ async def async_setup_entry(
"""Set up LCN light entities from a config entry."""
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
@ -93,11 +83,9 @@ class LcnOutputLight(LcnEntity, LightEntity):
_attr_is_on = False
_attr_brightness = 255
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN light."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.output = pypck.lcn_defs.OutputPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]]
@ -187,11 +175,9 @@ class LcnRelayLight(LcnEntity, LightEntity):
_attr_supported_color_modes = {ColorMode.ONOFF}
_attr_is_on = False
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN light."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.output = pypck.lcn_defs.RelayPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]]

View File

@ -8,12 +8,11 @@ import pypck
from homeassistant.components.scene import DOMAIN as DOMAIN_SCENE, Scene
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_ENTITIES, CONF_SCENE
from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES, CONF_SCENE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import (
ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA,
@ -23,27 +22,20 @@ from .const import (
DOMAIN,
OUTPUT_PORTS,
)
from .helpers import DeviceConnectionType, get_device_connection
from .entity import LcnEntity
PARALLEL_UPDATES = 0
def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
) -> None:
"""Add entities for this domain."""
entities: list[LcnScene] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
entities.append(
LcnScene(entity_config, config_entry.entry_id, device_connection)
)
entities = [
LcnScene(entity_config, config_entry) for entity_config in entity_configs
]
async_add_entities(entities)
@ -56,7 +48,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry."""
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
@ -77,11 +68,9 @@ async def async_setup_entry(
class LcnScene(LcnEntity, Scene):
"""Representation of a LCN scene."""
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN scene."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.register_id = config[CONF_DOMAIN_DATA][CONF_REGISTER]
self.scene_id = config[CONF_DOMAIN_DATA][CONF_SCENE]

View File

@ -10,7 +10,6 @@ import pypck
from homeassistant.components.sensor import DOMAIN as DOMAIN_SENSOR, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_ADDRESS,
CONF_DOMAIN,
CONF_ENTITIES,
CONF_SOURCE,
@ -20,7 +19,6 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import (
ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA,
@ -31,11 +29,11 @@ from .const import (
THRESHOLDS,
VARIABLES,
)
from .helpers import DeviceConnectionType, InputType, get_device_connection
from .entity import LcnEntity
from .helpers import InputType
def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
@ -43,24 +41,12 @@ def add_lcn_entities(
"""Add entities for this domain."""
entities: list[LcnVariableSensor | LcnLedLogicSensor] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in chain(
VARIABLES, SETPOINTS, THRESHOLDS, S0_INPUTS
):
entities.append(
LcnVariableSensor(
entity_config, config_entry.entry_id, device_connection
)
)
entities.append(LcnVariableSensor(entity_config, config_entry))
else: # in LED_PORTS + LOGICOP_PORTS
entities.append(
LcnLedLogicSensor(
entity_config, config_entry.entry_id, device_connection
)
)
entities.append(LcnLedLogicSensor(entity_config, config_entry))
async_add_entities(entities)
@ -73,7 +59,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry."""
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
@ -94,11 +79,9 @@ async def async_setup_entry(
class LcnVariableSensor(LcnEntity, SensorEntity):
"""Representation of a LCN sensor for variables."""
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN sensor."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.variable = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
self.unit = pypck.lcn_defs.VarUnit.parse(
@ -133,11 +116,9 @@ class LcnVariableSensor(LcnEntity, SensorEntity):
class LcnLedLogicSensor(LcnEntity, SensorEntity):
"""Representation of a LCN sensor for leds and logicops."""
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN sensor."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
if config[CONF_DOMAIN_DATA][CONF_SOURCE] in LED_PORTS:
self.source = pypck.lcn_defs.LedPort[config[CONF_DOMAIN_DATA][CONF_SOURCE]]

View File

@ -8,12 +8,11 @@ import pypck
from homeassistant.components.switch import DOMAIN as DOMAIN_SWITCH, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_ENTITIES
from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import (
ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA,
@ -21,13 +20,13 @@ from .const import (
DOMAIN,
OUTPUT_PORTS,
)
from .helpers import DeviceConnectionType, InputType, get_device_connection
from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0
def add_lcn_switch_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
@ -35,18 +34,10 @@ def add_lcn_switch_entities(
"""Add entities for this domain."""
entities: list[LcnOutputSwitch | LcnRelaySwitch] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS:
entities.append(
LcnOutputSwitch(entity_config, config_entry.entry_id, device_connection)
)
entities.append(LcnOutputSwitch(entity_config, config_entry))
else: # in RELAY_PORTS
entities.append(
LcnRelaySwitch(entity_config, config_entry.entry_id, device_connection)
)
entities.append(LcnRelaySwitch(entity_config, config_entry))
async_add_entities(entities)
@ -59,7 +50,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry."""
add_entities = partial(
add_lcn_switch_entities,
hass,
config_entry,
async_add_entities,
)
@ -82,11 +72,9 @@ class LcnOutputSwitch(LcnEntity, SwitchEntity):
_attr_is_on = False
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN switch."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.output = pypck.lcn_defs.OutputPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]]
@ -133,11 +121,9 @@ class LcnRelaySwitch(LcnEntity, SwitchEntity):
_attr_is_on = False
def __init__(
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
"""Initialize the LCN switch."""
super().__init__(config, entry_id, device_connection)
super().__init__(config, config_entry)
self.output = pypck.lcn_defs.RelayPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]]