2019-02-14 05:35:12 +01:00
|
|
|
"""Support for Modbus switches."""
|
2021-03-18 13:07:04 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-04-15 16:47:42 +02:00
|
|
|
import logging
|
2019-02-24 10:22:17 +01:00
|
|
|
|
2021-04-30 16:47:18 +02:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-05-24 20:13:25 +02:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_SWITCHES
|
2021-04-19 10:13:32 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2015-04-15 16:47:42 +02:00
|
|
|
|
2021-08-08 22:48:33 +02:00
|
|
|
from . import get_hub
|
2021-05-24 20:13:25 +02:00
|
|
|
from .base_platform import BaseSwitch
|
2021-02-12 16:33:18 +01:00
|
|
|
from .modbus import ModbusHub
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2021-05-15 19:54:17 +02:00
|
|
|
PARALLEL_UPDATES = 1
|
2015-04-15 16:47:42 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-02-14 05:35:12 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2020-10-12 14:34:44 +02:00
|
|
|
async def async_setup_platform(
|
2021-04-19 10:13:32 +02:00
|
|
|
hass: HomeAssistant, config: ConfigType, async_add_entities, discovery_info=None
|
2020-10-12 14:34:44 +02:00
|
|
|
):
|
|
|
|
"""Read configuration and create Modbus switches."""
|
2015-04-15 16:47:42 +02:00
|
|
|
switches = []
|
|
|
|
|
2021-05-28 11:29:37 +02:00
|
|
|
if discovery_info is None: # pragma: no cover
|
|
|
|
return
|
|
|
|
|
2021-03-27 22:48:06 +01:00
|
|
|
for entry in discovery_info[CONF_SWITCHES]:
|
2021-08-08 22:48:33 +02:00
|
|
|
hub: ModbusHub = get_hub(hass, discovery_info[CONF_NAME])
|
2021-04-30 16:47:18 +02:00
|
|
|
switches.append(ModbusSwitch(hub, entry))
|
2020-10-12 14:34:44 +02:00
|
|
|
async_add_entities(switches)
|
|
|
|
|
|
|
|
|
2021-05-24 20:13:25 +02:00
|
|
|
class ModbusSwitch(BaseSwitch, SwitchEntity):
|
2020-10-12 14:34:44 +02:00
|
|
|
"""Base class representing a Modbus switch."""
|
|
|
|
|
2021-05-24 12:59:55 +02:00
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Set switch on."""
|
2021-05-24 20:13:25 +02:00
|
|
|
await self.async_turn(self.command_on)
|