2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Modbus switches."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-23 17:35:50 +00:00
|
|
|
from typing import Any
|
2019-02-24 09:22:17 +00:00
|
|
|
|
2021-04-30 14:47:18 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-05-24 18:13:25 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_SWITCHES
|
2021-04-19 08:13:32 +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
|
2015-04-15 14:47:42 +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-02-12 15:33:18 +00:00
|
|
|
from .modbus import ModbusHub
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
PARALLEL_UPDATES = 1
|
2019-02-14 04:35:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-10-12 12:34:44 +00:00
|
|
|
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:
|
2020-10-12 12:34:44 +00:00
|
|
|
"""Read configuration and create Modbus switches."""
|
2015-04-15 14:47:42 +00:00
|
|
|
switches = []
|
|
|
|
|
2022-03-11 17:58:18 +00:00
|
|
|
if discovery_info is None:
|
2021-05-28 09:29:37 +00:00
|
|
|
return
|
|
|
|
|
2021-03-27 21:48:06 +00:00
|
|
|
for entry in discovery_info[CONF_SWITCHES]:
|
2021-08-08 20:48:33 +00:00
|
|
|
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
|
2021-04-30 14:47:18 +00:00
|
|
|
switches.append(ModbusSwitch(hub, entry))
|
2020-10-12 12:34:44 +00:00
|
|
|
async_add_entities(switches)
|
|
|
|
|
|
|
|
|
2021-05-24 18:13:25 +00:00
|
|
|
class ModbusSwitch(BaseSwitch, SwitchEntity):
|
2020-10-12 12:34:44 +00:00
|
|
|
"""Base class representing a Modbus switch."""
|
|
|
|
|
2021-09-23 17:35:50 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2021-05-24 10:59:55 +00:00
|
|
|
"""Set switch on."""
|
2021-05-24 18:13:25 +00:00
|
|
|
await self.async_turn(self.command_on)
|