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

pull/136488/head^2
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,
) -> None:
"""Read configuration and create Modbus climate."""
if discovery_info is None:
if discovery_info is None or not (climates := discovery_info[CONF_CLIMATES]):
return
entities = []
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)
hub = get_hub(hass, discovery_info[CONF_NAME])
async_add_entities(ModbusThermostat(hass, hub, config) for config in climates)
class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):

View File

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

View File

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

View File

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

View File

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