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 __future__ import annotations
from collections.abc import Callable
from functools import partial from functools import partial
import logging import logging
import pypck import pypck
from pypck.connection import PchkConnectionManager from pypck.connection import PchkConnectionManager
from homeassistant import config_entries from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_ADDRESS,
CONF_DEVICE_ID, CONF_DEVICE_ID,
CONF_DOMAIN,
CONF_IP_ADDRESS, CONF_IP_ADDRESS,
CONF_NAME,
CONF_PASSWORD, CONF_PASSWORD,
CONF_PORT, CONF_PORT,
CONF_RESOURCE,
CONF_USERNAME, CONF_USERNAME,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr 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 homeassistant.helpers.typing import ConfigType
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
CONF_DIM_MODE, CONF_DIM_MODE,
CONF_DOMAIN_DATA,
CONF_SK_NUM_TRIES, CONF_SK_NUM_TRIES,
CONNECTION, CONNECTION,
DOMAIN, DOMAIN,
@ -38,11 +30,9 @@ from .const import (
) )
from .helpers import ( from .helpers import (
AddressType, AddressType,
DeviceConnectionType,
InputType, InputType,
async_update_config_entry, async_update_config_entry,
generate_unique_id, generate_unique_id,
get_device_model,
import_lcn_config, import_lcn_config,
register_lcn_address_devices, register_lcn_address_devices,
register_lcn_host_device, register_lcn_host_device,
@ -67,16 +57,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.async_create_task( hass.async_create_task(
hass.config_entries.flow.async_init( hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_IMPORT}, context={"source": SOURCE_IMPORT},
data=config_entry_data, data=config_entry_data,
) )
) )
return True return True
async def async_setup_entry( async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
hass: HomeAssistant, config_entry: config_entries.ConfigEntry
) -> bool:
"""Set up a connection to PCHK host from a config entry.""" """Set up a connection to PCHK host from a config entry."""
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
if config_entry.entry_id in hass.data[DOMAIN]: if config_entry.entry_id in hass.data[DOMAIN]:
@ -149,9 +137,7 @@ async def async_setup_entry(
return True return True
async def async_unload_entry( async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
hass: HomeAssistant, config_entry: config_entries.ConfigEntry
) -> bool:
"""Close connection to PCHK host represented by config_entry.""" """Close connection to PCHK host represented by config_entry."""
# forward unloading to platforms # forward unloading to platforms
unload_ok = await hass.config_entries.async_unload_platforms( unload_ok = await hass.config_entries.async_unload_platforms(
@ -172,7 +158,7 @@ async def async_unload_entry(
def async_host_input_received( def async_host_input_received(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: config_entries.ConfigEntry, config_entry: ConfigEntry,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
inp: pypck.inputs.Input, inp: pypck.inputs.Input,
) -> None: ) -> None:
@ -242,75 +228,3 @@ def _async_fire_send_keys_event(
event_data.update({CONF_DEVICE_ID: device.id}) event_data.update({CONF_DEVICE_ID: device.id})
hass.bus.async_fire("lcn_send_keys", event_data) 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, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
BINSENSOR_PORTS, BINSENSOR_PORTS,
@ -23,11 +22,11 @@ from .const import (
DOMAIN, DOMAIN,
SETPOINTS, SETPOINTS,
) )
from .helpers import DeviceConnectionType, InputType, get_device_connection from .entity import LcnEntity
from .helpers import InputType
def add_lcn_entities( def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType], entity_configs: Iterable[ConfigType],
@ -35,26 +34,12 @@ def add_lcn_entities(
"""Add entities for this domain.""" """Add entities for this domain."""
entities: list[LcnRegulatorLockSensor | LcnBinarySensor | LcnLockKeysSensor] = [] entities: list[LcnRegulatorLockSensor | LcnBinarySensor | LcnLockKeysSensor] = []
for entity_config in entity_configs: 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: if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS:
entities.append( entities.append(LcnRegulatorLockSensor(entity_config, config_entry))
LcnRegulatorLockSensor(
entity_config, config_entry.entry_id, device_connection
)
)
elif entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS: elif entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS:
entities.append( entities.append(LcnBinarySensor(entity_config, config_entry))
LcnBinarySensor(entity_config, config_entry.entry_id, device_connection)
)
else: # in KEY else: # in KEY
entities.append( entities.append(LcnLockKeysSensor(entity_config, config_entry))
LcnLockKeysSensor(
entity_config, config_entry.entry_id, device_connection
)
)
async_add_entities(entities) async_add_entities(entities)
@ -67,7 +52,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry.""" """Set up LCN switch entities from a config entry."""
add_entities = partial( add_entities = partial(
add_lcn_entities, add_lcn_entities,
hass,
config_entry, config_entry,
async_add_entities, async_add_entities,
) )
@ -88,11 +72,9 @@ async def async_setup_entry(
class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity): class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity):
"""Representation of a LCN binary sensor for regulator locks.""" """Representation of a LCN binary sensor for regulator locks."""
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN binary sensor.""" """Initialize the LCN binary sensor."""
super().__init__(config, entry_id, device_connection) super().__init__(config, config_entry)
self.setpoint_variable = pypck.lcn_defs.Var[ self.setpoint_variable = pypck.lcn_defs.Var[
config[CONF_DOMAIN_DATA][CONF_SOURCE] config[CONF_DOMAIN_DATA][CONF_SOURCE]
@ -129,11 +111,9 @@ class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity):
class LcnBinarySensor(LcnEntity, BinarySensorEntity): class LcnBinarySensor(LcnEntity, BinarySensorEntity):
"""Representation of a LCN binary sensor for binary sensor ports.""" """Representation of a LCN binary sensor for binary sensor ports."""
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN binary sensor.""" """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[ self.bin_sensor_port = pypck.lcn_defs.BinSensorPort[
config[CONF_DOMAIN_DATA][CONF_SOURCE] config[CONF_DOMAIN_DATA][CONF_SOURCE]
@ -167,11 +147,9 @@ class LcnBinarySensor(LcnEntity, BinarySensorEntity):
class LcnLockKeysSensor(LcnEntity, BinarySensorEntity): class LcnLockKeysSensor(LcnEntity, BinarySensorEntity):
"""Representation of a LCN sensor for key locks.""" """Representation of a LCN sensor for key locks."""
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN sensor.""" """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]] 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.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
CONF_ADDRESS,
CONF_DOMAIN, CONF_DOMAIN,
CONF_ENTITIES, CONF_ENTITIES,
CONF_SOURCE, CONF_SOURCE,
@ -26,7 +25,6 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA, CONF_DOMAIN_DATA,
@ -36,27 +34,21 @@ from .const import (
CONF_SETPOINT, CONF_SETPOINT,
DOMAIN, DOMAIN,
) )
from .helpers import DeviceConnectionType, InputType, get_device_connection from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
def add_lcn_entities( def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType], entity_configs: Iterable[ConfigType],
) -> None: ) -> None:
"""Add entities for this domain.""" """Add entities for this domain."""
entities: list[LcnClimate] = [] entities = [
for entity_config in entity_configs: LcnClimate(entity_config, config_entry) 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)
)
async_add_entities(entities) async_add_entities(entities)
@ -69,7 +61,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry.""" """Set up LCN switch entities from a config entry."""
add_entities = partial( add_entities = partial(
add_lcn_entities, add_lcn_entities,
hass,
config_entry, config_entry,
async_add_entities, async_add_entities,
) )
@ -92,11 +83,9 @@ class LcnClimate(LcnEntity, ClimateEntity):
_enable_turn_on_off_backwards_compatibility = False _enable_turn_on_off_backwards_compatibility = False
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize of a LCN climate device.""" """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.variable = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
self.setpoint = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SETPOINT]] 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.components.cover import DOMAIN as DOMAIN_COVER, CoverEntity
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA, CONF_DOMAIN_DATA,
@ -21,13 +20,13 @@ from .const import (
CONF_REVERSE_TIME, CONF_REVERSE_TIME,
DOMAIN, DOMAIN,
) )
from .helpers import DeviceConnectionType, InputType, get_device_connection from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
def add_lcn_entities( def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType], entity_configs: Iterable[ConfigType],
@ -35,18 +34,10 @@ def add_lcn_entities(
"""Add entities for this domain.""" """Add entities for this domain."""
entities: list[LcnOutputsCover | LcnRelayCover] = [] entities: list[LcnOutputsCover | LcnRelayCover] = []
for entity_config in entity_configs: 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": if entity_config[CONF_DOMAIN_DATA][CONF_MOTOR] in "OUTPUTS":
entities.append( entities.append(LcnOutputsCover(entity_config, config_entry))
LcnOutputsCover(entity_config, config_entry.entry_id, device_connection)
)
else: # in RELAYS else: # in RELAYS
entities.append( entities.append(LcnRelayCover(entity_config, config_entry))
LcnRelayCover(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities) async_add_entities(entities)
@ -59,7 +50,6 @@ async def async_setup_entry(
"""Set up LCN cover entities from a config entry.""" """Set up LCN cover entities from a config entry."""
add_entities = partial( add_entities = partial(
add_lcn_entities, add_lcn_entities,
hass,
config_entry, config_entry,
async_add_entities, async_add_entities,
) )
@ -85,11 +75,9 @@ class LcnOutputsCover(LcnEntity, CoverEntity):
_attr_is_opening = False _attr_is_opening = False
_attr_assumed_state = True _attr_assumed_state = True
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN cover.""" """Initialize the LCN cover."""
super().__init__(config, entry_id, device_connection) super().__init__(config, config_entry)
self.output_ids = [ self.output_ids = [
pypck.lcn_defs.OutputPort["OUTPUTUP"].value, pypck.lcn_defs.OutputPort["OUTPUTUP"].value,
@ -189,11 +177,9 @@ class LcnRelayCover(LcnEntity, CoverEntity):
_attr_is_opening = False _attr_is_opening = False
_attr_assumed_state = True _attr_assumed_state = True
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN cover.""" """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 = pypck.lcn_defs.MotorPort[config[CONF_DOMAIN_DATA][CONF_MOTOR]]
self.motor_port_onoff = self.motor.value * 2 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( def get_device_connection(
hass: HomeAssistant, address: AddressType, config_entry: ConfigEntry hass: HomeAssistant, address: AddressType, config_entry: ConfigEntry
) -> DeviceConnectionType | None: ) -> DeviceConnectionType:
"""Return a lcn device_connection.""" """Return a lcn device_connection."""
host_connection = hass.data[DOMAIN][config_entry.entry_id][CONNECTION] host_connection = hass.data[DOMAIN][config_entry.entry_id][CONNECTION]
addr = pypck.lcn_addr.LcnAddr(*address) addr = pypck.lcn_addr.LcnAddr(*address)

View File

@ -15,12 +15,11 @@ from homeassistant.components.light import (
LightEntityFeature, LightEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
CONF_DIMMABLE, CONF_DIMMABLE,
@ -30,13 +29,13 @@ from .const import (
DOMAIN, DOMAIN,
OUTPUT_PORTS, OUTPUT_PORTS,
) )
from .helpers import DeviceConnectionType, InputType, get_device_connection from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
def add_lcn_entities( def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType], entity_configs: Iterable[ConfigType],
@ -44,18 +43,10 @@ def add_lcn_entities(
"""Add entities for this domain.""" """Add entities for this domain."""
entities: list[LcnOutputLight | LcnRelayLight] = [] entities: list[LcnOutputLight | LcnRelayLight] = []
for entity_config in entity_configs: 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: if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS:
entities.append( entities.append(LcnOutputLight(entity_config, config_entry))
LcnOutputLight(entity_config, config_entry.entry_id, device_connection)
)
else: # in RELAY_PORTS else: # in RELAY_PORTS
entities.append( entities.append(LcnRelayLight(entity_config, config_entry))
LcnRelayLight(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities) async_add_entities(entities)
@ -68,7 +59,6 @@ async def async_setup_entry(
"""Set up LCN light entities from a config entry.""" """Set up LCN light entities from a config entry."""
add_entities = partial( add_entities = partial(
add_lcn_entities, add_lcn_entities,
hass,
config_entry, config_entry,
async_add_entities, async_add_entities,
) )
@ -93,11 +83,9 @@ class LcnOutputLight(LcnEntity, LightEntity):
_attr_is_on = False _attr_is_on = False
_attr_brightness = 255 _attr_brightness = 255
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN light.""" """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]] 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_supported_color_modes = {ColorMode.ONOFF}
_attr_is_on = False _attr_is_on = False
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN light.""" """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]] 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.components.scene import DOMAIN as DOMAIN_SCENE, Scene
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA, CONF_DOMAIN_DATA,
@ -23,27 +22,20 @@ from .const import (
DOMAIN, DOMAIN,
OUTPUT_PORTS, OUTPUT_PORTS,
) )
from .helpers import DeviceConnectionType, get_device_connection from .entity import LcnEntity
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
def add_lcn_entities( def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType], entity_configs: Iterable[ConfigType],
) -> None: ) -> None:
"""Add entities for this domain.""" """Add entities for this domain."""
entities: list[LcnScene] = [] entities = [
for entity_config in entity_configs: LcnScene(entity_config, config_entry) 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)
)
async_add_entities(entities) async_add_entities(entities)
@ -56,7 +48,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry.""" """Set up LCN switch entities from a config entry."""
add_entities = partial( add_entities = partial(
add_lcn_entities, add_lcn_entities,
hass,
config_entry, config_entry,
async_add_entities, async_add_entities,
) )
@ -77,11 +68,9 @@ async def async_setup_entry(
class LcnScene(LcnEntity, Scene): class LcnScene(LcnEntity, Scene):
"""Representation of a LCN scene.""" """Representation of a LCN scene."""
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN scene.""" """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.register_id = config[CONF_DOMAIN_DATA][CONF_REGISTER]
self.scene_id = config[CONF_DOMAIN_DATA][CONF_SCENE] 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.components.sensor import DOMAIN as DOMAIN_SENSOR, SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_ADDRESS,
CONF_DOMAIN, CONF_DOMAIN,
CONF_ENTITIES, CONF_ENTITIES,
CONF_SOURCE, CONF_SOURCE,
@ -20,7 +19,6 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA, CONF_DOMAIN_DATA,
@ -31,11 +29,11 @@ from .const import (
THRESHOLDS, THRESHOLDS,
VARIABLES, VARIABLES,
) )
from .helpers import DeviceConnectionType, InputType, get_device_connection from .entity import LcnEntity
from .helpers import InputType
def add_lcn_entities( def add_lcn_entities(
hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType], entity_configs: Iterable[ConfigType],
@ -43,24 +41,12 @@ def add_lcn_entities(
"""Add entities for this domain.""" """Add entities for this domain."""
entities: list[LcnVariableSensor | LcnLedLogicSensor] = [] entities: list[LcnVariableSensor | LcnLedLogicSensor] = []
for entity_config in entity_configs: 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( if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in chain(
VARIABLES, SETPOINTS, THRESHOLDS, S0_INPUTS VARIABLES, SETPOINTS, THRESHOLDS, S0_INPUTS
): ):
entities.append( entities.append(LcnVariableSensor(entity_config, config_entry))
LcnVariableSensor(
entity_config, config_entry.entry_id, device_connection
)
)
else: # in LED_PORTS + LOGICOP_PORTS else: # in LED_PORTS + LOGICOP_PORTS
entities.append( entities.append(LcnLedLogicSensor(entity_config, config_entry))
LcnLedLogicSensor(
entity_config, config_entry.entry_id, device_connection
)
)
async_add_entities(entities) async_add_entities(entities)
@ -73,7 +59,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry.""" """Set up LCN switch entities from a config entry."""
add_entities = partial( add_entities = partial(
add_lcn_entities, add_lcn_entities,
hass,
config_entry, config_entry,
async_add_entities, async_add_entities,
) )
@ -94,11 +79,9 @@ async def async_setup_entry(
class LcnVariableSensor(LcnEntity, SensorEntity): class LcnVariableSensor(LcnEntity, SensorEntity):
"""Representation of a LCN sensor for variables.""" """Representation of a LCN sensor for variables."""
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN sensor.""" """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.variable = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
self.unit = pypck.lcn_defs.VarUnit.parse( self.unit = pypck.lcn_defs.VarUnit.parse(
@ -133,11 +116,9 @@ class LcnVariableSensor(LcnEntity, SensorEntity):
class LcnLedLogicSensor(LcnEntity, SensorEntity): class LcnLedLogicSensor(LcnEntity, SensorEntity):
"""Representation of a LCN sensor for leds and logicops.""" """Representation of a LCN sensor for leds and logicops."""
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN sensor.""" """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: if config[CONF_DOMAIN_DATA][CONF_SOURCE] in LED_PORTS:
self.source = pypck.lcn_defs.LedPort[config[CONF_DOMAIN_DATA][CONF_SOURCE]] 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.components.switch import DOMAIN as DOMAIN_SWITCH, SwitchEntity
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import LcnEntity
from .const import ( from .const import (
ADD_ENTITIES_CALLBACKS, ADD_ENTITIES_CALLBACKS,
CONF_DOMAIN_DATA, CONF_DOMAIN_DATA,
@ -21,13 +20,13 @@ from .const import (
DOMAIN, DOMAIN,
OUTPUT_PORTS, OUTPUT_PORTS,
) )
from .helpers import DeviceConnectionType, InputType, get_device_connection from .entity import LcnEntity
from .helpers import InputType
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
def add_lcn_switch_entities( def add_lcn_switch_entities(
hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType], entity_configs: Iterable[ConfigType],
@ -35,18 +34,10 @@ def add_lcn_switch_entities(
"""Add entities for this domain.""" """Add entities for this domain."""
entities: list[LcnOutputSwitch | LcnRelaySwitch] = [] entities: list[LcnOutputSwitch | LcnRelaySwitch] = []
for entity_config in entity_configs: 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: if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS:
entities.append( entities.append(LcnOutputSwitch(entity_config, config_entry))
LcnOutputSwitch(entity_config, config_entry.entry_id, device_connection)
)
else: # in RELAY_PORTS else: # in RELAY_PORTS
entities.append( entities.append(LcnRelaySwitch(entity_config, config_entry))
LcnRelaySwitch(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities) async_add_entities(entities)
@ -59,7 +50,6 @@ async def async_setup_entry(
"""Set up LCN switch entities from a config entry.""" """Set up LCN switch entities from a config entry."""
add_entities = partial( add_entities = partial(
add_lcn_switch_entities, add_lcn_switch_entities,
hass,
config_entry, config_entry,
async_add_entities, async_add_entities,
) )
@ -82,11 +72,9 @@ class LcnOutputSwitch(LcnEntity, SwitchEntity):
_attr_is_on = False _attr_is_on = False
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN switch.""" """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]] self.output = pypck.lcn_defs.OutputPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]]
@ -133,11 +121,9 @@ class LcnRelaySwitch(LcnEntity, SwitchEntity):
_attr_is_on = False _attr_is_on = False
def __init__( def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType
) -> None:
"""Initialize the LCN switch.""" """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]] self.output = pypck.lcn_defs.RelayPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]]