Skip to content

Commit

Permalink
Reduce boilerplate code to setup modbus platform entities (#136491)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 25, 2025
1 parent 2fb85aa commit 772f61c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 40 deletions.
11 changes: 3 additions & 8 deletions homeassistant/components/modbus/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 3 additions & 8 deletions homeassistant/components/modbus/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 3 additions & 7 deletions homeassistant/components/modbus/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 3 additions & 8 deletions homeassistant/components/modbus/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from . import get_hub
from .entity import BaseSwitch
from .modbus import ModbusHub

PARALLEL_UPDATES = 1

Expand All @@ -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):
Expand Down
12 changes: 3 additions & 9 deletions homeassistant/components/modbus/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from . import get_hub
from .entity import BaseSwitch
from .modbus import ModbusHub

PARALLEL_UPDATES = 1

Expand All @@ -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):
Expand Down

0 comments on commit 772f61c

Please sign in to comment.