diff --git a/homeassistant/components/lcn/binary_sensor.py b/homeassistant/components/lcn/binary_sensor.py index d7e5798bb91..a0f8e1cf360 100644 --- a/homeassistant/components/lcn/binary_sensor.py +++ b/homeassistant/components/lcn/binary_sensor.py @@ -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 + ), ) diff --git a/homeassistant/components/lcn/climate.py b/homeassistant/components/lcn/climate.py index d34a872d867..0142894a16b 100644 --- a/homeassistant/components/lcn/climate.py +++ b/homeassistant/components/lcn/climate.py @@ -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 + ), ) diff --git a/homeassistant/components/lcn/cover.py b/homeassistant/components/lcn/cover.py index a2f508cee97..1e428a350d6 100644 --- a/homeassistant/components/lcn/cover.py +++ b/homeassistant/components/lcn/cover.py @@ -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 + ), ) diff --git a/homeassistant/components/lcn/light.py b/homeassistant/components/lcn/light.py index b896462c8a1..799ed0036d8 100644 --- a/homeassistant/components/lcn/light.py +++ b/homeassistant/components/lcn/light.py @@ -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 + ), ) diff --git a/homeassistant/components/lcn/scene.py b/homeassistant/components/lcn/scene.py index f9220f676d6..52ec0262b55 100644 --- a/homeassistant/components/lcn/scene.py +++ b/homeassistant/components/lcn/scene.py @@ -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 + ), ) diff --git a/homeassistant/components/lcn/sensor.py b/homeassistant/components/lcn/sensor.py index b63ddbef8ad..7e8941a0bf9 100644 --- a/homeassistant/components/lcn/sensor.py +++ b/homeassistant/components/lcn/sensor.py @@ -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 + ), ) diff --git a/homeassistant/components/lcn/switch.py b/homeassistant/components/lcn/switch.py index 1136e4c27a1..4c316cef547 100644 --- a/homeassistant/components/lcn/switch.py +++ b/homeassistant/components/lcn/switch.py @@ -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 + ), ) diff --git a/homeassistant/components/lcn/websocket.py b/homeassistant/components/lcn/websocket.py index 6fda20aba93..b418e362b27 100644 --- a/homeassistant/components/lcn/websocket.py +++ b/homeassistant/components/lcn/websocket.py @@ -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]