Reduce boilerplate code to setup modbus platform entities (#136491)

This commit is contained in:
J. Nick Koston 2025-01-25 07:14:06 -10:00 committed by GitHub
parent 2fb85aab8e
commit 772f61cf77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 15 additions and 40 deletions

View File

@ -112,15 +112,10 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Read configuration and create Modbus climate.""" """Read configuration and create Modbus climate."""
if discovery_info is None: if discovery_info is None or not (climates := discovery_info[CONF_CLIMATES]):
return return
hub = get_hub(hass, discovery_info[CONF_NAME])
entities = [] async_add_entities(ModbusThermostat(hass, hub, config) for config in climates)
for entity in discovery_info[CONF_CLIMATES]:
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
entities.append(ModbusThermostat(hass, hub, entity))
async_add_entities(entities)
class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity): class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):

View File

@ -36,15 +36,10 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Read configuration and create Modbus cover.""" """Read configuration and create Modbus cover."""
if discovery_info is None: if discovery_info is None or not (covers := discovery_info[CONF_COVERS]):
return return
hub = get_hub(hass, discovery_info[CONF_NAME])
covers = [] async_add_entities(ModbusCover(hass, hub, config) for config in covers)
for cover in discovery_info[CONF_COVERS]:
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
covers.append(ModbusCover(hass, hub, cover))
async_add_entities(covers)
class ModbusCover(BasePlatform, CoverEntity, RestoreEntity): class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):

View File

@ -25,14 +25,10 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Read configuration and create Modbus fans.""" """Read configuration and create Modbus fans."""
if discovery_info is None: if discovery_info is None or not (fans := discovery_info[CONF_FANS]):
return return
fans = [] hub = get_hub(hass, discovery_info[CONF_NAME])
async_add_entities(ModbusFan(hass, hub, config) for config in fans)
for entry in discovery_info[CONF_FANS]:
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
fans.append(ModbusFan(hass, hub, entry))
async_add_entities(fans)
class ModbusFan(BaseSwitch, FanEntity): class ModbusFan(BaseSwitch, FanEntity):

View File

@ -12,7 +12,6 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import get_hub from . import get_hub
from .entity import BaseSwitch from .entity import BaseSwitch
from .modbus import ModbusHub
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
@ -24,14 +23,10 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Read configuration and create Modbus lights.""" """Read configuration and create Modbus lights."""
if discovery_info is None: if discovery_info is None or not (lights := discovery_info[CONF_LIGHTS]):
return return
hub = get_hub(hass, discovery_info[CONF_NAME])
lights = [] async_add_entities(ModbusLight(hass, hub, config) for config in lights)
for entry in discovery_info[CONF_LIGHTS]:
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
lights.append(ModbusLight(hass, hub, entry))
async_add_entities(lights)
class ModbusLight(BaseSwitch, LightEntity): class ModbusLight(BaseSwitch, LightEntity):

View File

@ -12,7 +12,6 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import get_hub from . import get_hub
from .entity import BaseSwitch from .entity import BaseSwitch
from .modbus import ModbusHub
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
@ -24,15 +23,10 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Read configuration and create Modbus switches.""" """Read configuration and create Modbus switches."""
switches = [] if discovery_info is None or not (switches := discovery_info[CONF_SWITCHES]):
if discovery_info is None:
return return
hub = get_hub(hass, discovery_info[CONF_NAME])
for entry in discovery_info[CONF_SWITCHES]: async_add_entities(ModbusSwitch(hass, hub, config) for config in switches)
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
switches.append(ModbusSwitch(hass, hub, entry))
async_add_entities(switches)
class ModbusSwitch(BaseSwitch, SwitchEntity): class ModbusSwitch(BaseSwitch, SwitchEntity):