Skip to content

Commit

Permalink
Refactor LcnEntity signature (#124411)
Browse files Browse the repository at this point in the history
* Refactorings due to change of LcnEntity signature

* Fix PR comments

* Move parent class LcnEntity to entity.py
  • Loading branch information
alengwenus authored Sep 10, 2024
1 parent aa8f983 commit 2b3a6e5
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 258 deletions.
96 changes: 5 additions & 91 deletions homeassistant/components/lcn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,37 @@

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,
PLATFORMS,
)
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,
Expand All @@ -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]:
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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."""
46 changes: 12 additions & 34 deletions homeassistant/components/lcn/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,36 @@
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,
CONF_DOMAIN_DATA,
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],
) -> None:
"""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)

Expand All @@ -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,
)
Expand All @@ -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]
Expand Down Expand Up @@ -129,11 +111,9 @@ def input_received(self, input_obj: InputType) -> None:
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]
Expand Down Expand Up @@ -167,11 +147,9 @@ def input_received(self, input_obj: InputType) -> None:
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]]

Expand Down
25 changes: 7 additions & 18 deletions homeassistant/components/lcn/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_ADDRESS,
CONF_DOMAIN,
CONF_ENTITIES,
CONF_SOURCE,
Expand All @@ -26,7 +25,6 @@
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,
Expand All @@ -36,27 +34,21 @@
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)

Expand All @@ -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,
)
Expand All @@ -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]]
Expand Down
Loading

0 comments on commit 2b3a6e5

Please sign in to comment.