Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce boilerplate code to setup modbus platform entities #136491

Merged
merged 3 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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