2021-05-21 06:57:17 +00:00
|
|
|
"""Support for Modbus lights."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
2021-05-21 06:57:17 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-23 17:35:50 +00:00
|
|
|
from typing import Any
|
2021-05-21 06:57:17 +00:00
|
|
|
|
2022-04-23 19:24:06 +00:00
|
|
|
from homeassistant.components.light import ColorMode, LightEntity
|
2021-05-24 18:13:25 +00:00
|
|
|
from homeassistant.const import CONF_LIGHTS, CONF_NAME
|
2021-05-21 06:57:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-09-23 17:35:50 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2021-05-21 06:57:17 +00:00
|
|
|
|
2021-08-08 20:48:33 +00:00
|
|
|
from . import get_hub
|
2021-05-24 18:13:25 +00:00
|
|
|
from .base_platform import BaseSwitch
|
2021-05-21 06:57:17 +00:00
|
|
|
from .modbus import ModbusHub
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
2021-09-23 17:35:50 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2021-05-21 06:57:17 +00:00
|
|
|
"""Read configuration and create Modbus lights."""
|
2022-03-11 17:58:18 +00:00
|
|
|
if discovery_info is None:
|
2021-05-21 06:57:17 +00:00
|
|
|
return
|
2021-05-28 09:29:37 +00:00
|
|
|
|
2021-05-21 06:57:17 +00:00
|
|
|
lights = []
|
|
|
|
for entry in discovery_info[CONF_LIGHTS]:
|
2021-08-08 20:48:33 +00:00
|
|
|
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
|
2023-12-05 12:08:33 +00:00
|
|
|
lights.append(ModbusLight(hass, hub, entry))
|
2021-05-21 06:57:17 +00:00
|
|
|
async_add_entities(lights)
|
|
|
|
|
|
|
|
|
2021-05-24 18:13:25 +00:00
|
|
|
class ModbusLight(BaseSwitch, LightEntity):
|
|
|
|
"""Class representing a Modbus light."""
|
2021-05-21 06:57:17 +00:00
|
|
|
|
2022-04-23 19:24:06 +00:00
|
|
|
_attr_color_mode = ColorMode.ONOFF
|
|
|
|
_attr_supported_color_modes = {ColorMode.ONOFF}
|
2022-04-06 18:27:03 +00:00
|
|
|
|
2021-09-23 17:35:50 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2021-05-21 06:57:17 +00:00
|
|
|
"""Set light on."""
|
2021-05-24 18:13:25 +00:00
|
|
|
await self.async_turn(self.command_on)
|