2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Modbus switches."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-04-15 14:47:42 +00:00
|
|
|
import logging
|
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
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2021-05-24 18:13:25 +00:00
|
|
|
from .base_platform import BaseSwitch
|
|
|
|
from .const import MODBUS_DOMAIN
|
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
|
2015-04-15 14:47:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
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-04-19 08:13:32 +00:00
|
|
|
hass: HomeAssistant, config: ConfigType, async_add_entities, discovery_info=None
|
2020-10-12 12:34:44 +00:00
|
|
|
):
|
|
|
|
"""Read configuration and create Modbus switches."""
|
2015-04-15 14:47:42 +00:00
|
|
|
switches = []
|
|
|
|
|
2021-05-28 09:29:37 +00:00
|
|
|
if discovery_info is None: # pragma: no cover
|
|
|
|
return
|
|
|
|
|
2021-03-27 21:48:06 +00:00
|
|
|
for entry in discovery_info[CONF_SWITCHES]:
|
2021-04-30 14:47:18 +00:00
|
|
|
hub: ModbusHub = hass.data[MODBUS_DOMAIN][discovery_info[CONF_NAME]]
|
|
|
|
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-05-24 10:59:55 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Set switch on."""
|
2021-05-24 18:13:25 +00:00
|
|
|
await self.async_turn(self.command_on)
|