Handle LCN entity instances only in corresponding platform (#124589)

* Handle switch entity instance only in switch platform

* Add other platforms
This commit is contained in:
Andre Lengwenus 2024-08-26 10:17:51 +02:00 committed by GitHub
parent 302ffe5e56
commit 7ddd755acc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 281 additions and 139 deletions

View File

@ -1,6 +1,7 @@
"""Support for LCN binary sensors."""
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
import pypck
@ -25,22 +26,37 @@ from .const import (
from .helpers import DeviceConnectionType, InputType, get_device_connection
def create_lcn_binary_sensor_entity(
hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry
) -> LcnEntity:
"""Set up an entity for this domain."""
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS:
return LcnRegulatorLockSensor(
entity_config, config_entry.entry_id, device_connection
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 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 entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS:
entities.append(
LcnRegulatorLockSensor(
entity_config, config_entry.entry_id, device_connection
)
)
elif entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS:
entities.append(
LcnBinarySensor(entity_config, config_entry.entry_id, device_connection)
)
else: # in KEY
entities.append(
LcnLockKeysSensor(
entity_config, config_entry.entry_id, device_connection
)
)
async_add_entities(entities)
async def async_setup_entry(
@ -49,14 +65,23 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LCN switch entities from a config entry."""
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_BINARY_SENSOR: (async_add_entities, create_lcn_binary_sensor_entity)}
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
async_add_entities(
create_lcn_binary_sensor_entity(hass, entity_config, config_entry)
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_BINARY_SENSOR
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_BINARY_SENSOR: add_entities}
)
add_entities(
(
entity_config
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_BINARY_SENSOR
),
)

View File

@ -1,7 +1,7 @@
"""Support for LCN climate control."""
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
from typing import Any, cast
import pypck
@ -41,15 +41,24 @@ from .helpers import DeviceConnectionType, InputType, get_device_connection
PARALLEL_UPDATES = 0
def create_lcn_climate_entity(
hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry
) -> LcnEntity:
"""Set up an entity for this domain."""
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
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
)
return LcnClimate(entity_config, config_entry.entry_id, device_connection)
entities.append(
LcnClimate(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities)
async def async_setup_entry(
@ -58,14 +67,23 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LCN switch entities from a config entry."""
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_CLIMATE: (async_add_entities, create_lcn_climate_entity)}
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
async_add_entities(
create_lcn_climate_entity(hass, entity_config, config_entry)
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_CLIMATE
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_CLIMATE: add_entities}
)
add_entities(
(
entity_config
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_CLIMATE
),
)

View File

@ -1,7 +1,7 @@
"""Support for LCN covers."""
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
from typing import Any
import pypck
@ -26,18 +26,29 @@ from .helpers import DeviceConnectionType, InputType, get_device_connection
PARALLEL_UPDATES = 0
def create_lcn_cover_entity(
hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry
) -> LcnEntity:
"""Set up an entity for this domain."""
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
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[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":
return LcnOutputsCover(entity_config, config_entry.entry_id, device_connection)
# in RELAYS
return LcnRelayCover(entity_config, config_entry.entry_id, device_connection)
if entity_config[CONF_DOMAIN_DATA][CONF_MOTOR] in "OUTPUTS":
entities.append(
LcnOutputsCover(entity_config, config_entry.entry_id, device_connection)
)
else: # in RELAYS
entities.append(
LcnRelayCover(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities)
async def async_setup_entry(
@ -46,14 +57,23 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LCN cover entities from a config entry."""
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_COVER: (async_add_entities, create_lcn_cover_entity)}
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
async_add_entities(
create_lcn_cover_entity(hass, entity_config, config_entry)
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_COVER
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_COVER: add_entities}
)
add_entities(
(
entity_config
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_COVER
),
)

View File

@ -1,7 +1,7 @@
"""Support for LCN lights."""
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
from typing import Any
import pypck
@ -35,18 +35,29 @@ from .helpers import DeviceConnectionType, InputType, get_device_connection
PARALLEL_UPDATES = 0
def create_lcn_light_entity(
hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry
) -> LcnEntity:
"""Set up an entity for this domain."""
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
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[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:
return LcnOutputLight(entity_config, config_entry.entry_id, device_connection)
# in RELAY_PORTS
return LcnRelayLight(entity_config, config_entry.entry_id, device_connection)
if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS:
entities.append(
LcnOutputLight(entity_config, config_entry.entry_id, device_connection)
)
else: # in RELAY_PORTS
entities.append(
LcnRelayLight(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities)
async def async_setup_entry(
@ -55,14 +66,23 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LCN light entities from a config entry."""
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_LIGHT: (async_add_entities, create_lcn_light_entity)}
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
async_add_entities(
create_lcn_light_entity(hass, entity_config, config_entry)
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_LIGHT
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_LIGHT: add_entities}
)
add_entities(
(
entity_config
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_LIGHT
),
)

View File

@ -1,7 +1,7 @@
"""Support for LCN scenes."""
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
from typing import Any
import pypck
@ -28,15 +28,24 @@ from .helpers import DeviceConnectionType, get_device_connection
PARALLEL_UPDATES = 0
def create_lcn_scene_entity(
hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry
) -> LcnEntity:
"""Set up an entity for this domain."""
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
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
)
return LcnScene(entity_config, config_entry.entry_id, device_connection)
entities.append(
LcnScene(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities)
async def async_setup_entry(
@ -45,14 +54,23 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LCN switch entities from a config entry."""
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_SCENE: (async_add_entities, create_lcn_scene_entity)}
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
async_add_entities(
create_lcn_scene_entity(hass, entity_config, config_entry)
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_SCENE
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_SCENE: add_entities}
)
add_entities(
(
entity_config
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_SCENE
),
)

View File

@ -1,7 +1,7 @@
"""Support for LCN sensors."""
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
from itertools import chain
from typing import cast
@ -34,22 +34,35 @@ from .const import (
from .helpers import DeviceConnectionType, InputType, get_device_connection
def create_lcn_sensor_entity(
hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry
) -> LcnEntity:
"""Set up an entity for this domain."""
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
):
return LcnVariableSensor(
entity_config, config_entry.entry_id, device_connection
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[LcnVariableSensor | LcnLedLogicSensor] = []
for entity_config in entity_configs:
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
# in LED_PORTS + LOGICOP_PORTS
return LcnLedLogicSensor(entity_config, config_entry.entry_id, device_connection)
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
)
)
else: # in LED_PORTS + LOGICOP_PORTS
entities.append(
LcnLedLogicSensor(
entity_config, config_entry.entry_id, device_connection
)
)
async_add_entities(entities)
async def async_setup_entry(
@ -58,14 +71,23 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LCN switch entities from a config entry."""
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_SENSOR: (async_add_entities, create_lcn_sensor_entity)}
add_entities = partial(
add_lcn_entities,
hass,
config_entry,
async_add_entities,
)
async_add_entities(
create_lcn_sensor_entity(hass, entity_config, config_entry)
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_SENSOR
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_SENSOR: add_entities}
)
add_entities(
(
entity_config
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_SENSOR
),
)

View File

@ -1,7 +1,7 @@
"""Support for LCN switches."""
from __future__ import annotations
from collections.abc import Iterable
from functools import partial
from typing import Any
import pypck
@ -26,18 +26,29 @@ from .helpers import DeviceConnectionType, InputType, get_device_connection
PARALLEL_UPDATES = 0
def create_lcn_switch_entity(
hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry
) -> LcnEntity:
"""Set up an entity for this domain."""
device_connection = get_device_connection(
hass, entity_config[CONF_ADDRESS], config_entry
)
def add_lcn_switch_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
entity_configs: Iterable[ConfigType],
) -> None:
"""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:
return LcnOutputSwitch(entity_config, config_entry.entry_id, device_connection)
# in RELAY_PORTS
return LcnRelaySwitch(entity_config, config_entry.entry_id, device_connection)
if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS:
entities.append(
LcnOutputSwitch(entity_config, config_entry.entry_id, device_connection)
)
else: # in RELAY_PORTS
entities.append(
LcnRelaySwitch(entity_config, config_entry.entry_id, device_connection)
)
async_add_entities(entities)
async def async_setup_entry(
@ -46,14 +57,23 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LCN switch entities from a config entry."""
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_SWITCH: (async_add_entities, create_lcn_switch_entity)}
add_entities = partial(
add_lcn_switch_entities,
hass,
config_entry,
async_add_entities,
)
async_add_entities(
create_lcn_switch_entity(hass, entity_config, config_entry)
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_SWITCH
hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
{DOMAIN_SWITCH: add_entities}
)
add_entities(
(
entity_config
for entity_config in config_entry.data[CONF_ENTITIES]
if entity_config[CONF_DOMAIN] == DOMAIN_SWITCH
),
)

View File

@ -341,11 +341,10 @@ async def websocket_add_entity(
}
# Create new entity and add to corresponding component
callbacks = hass.data[DOMAIN][msg["entry_id"]][ADD_ENTITIES_CALLBACKS]
async_add_entities, create_lcn_entity = callbacks[msg[CONF_DOMAIN]]
entity = create_lcn_entity(hass, entity_config, config_entry)
async_add_entities([entity])
add_entities = hass.data[DOMAIN][msg["entry_id"]][ADD_ENTITIES_CALLBACKS][
msg[CONF_DOMAIN]
]
add_entities([entity_config])
# Add entity config to config_entry
entity_configs = [*config_entry.data[CONF_ENTITIES], entity_config]