2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Velbus switches."""
|
2021-09-13 06:22:46 +00:00
|
|
|
from typing import Any
|
2019-07-30 10:56:40 +00:00
|
|
|
|
2021-11-11 12:46:35 +00:00
|
|
|
from velbusaio.channels import Relay as VelbusRelay
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-11-04 13:37:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
from . import VelbusEntity
|
|
|
|
from .const import DOMAIN
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
|
2021-11-04 13:37:44 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-07-29 07:21:26 +00:00
|
|
|
"""Set up Velbus switch based on config_entry."""
|
2021-09-13 06:22:46 +00:00
|
|
|
await hass.data[DOMAIN][entry.entry_id]["tsk"]
|
2019-07-31 19:25:30 +00:00
|
|
|
cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
|
2019-07-29 07:21:26 +00:00
|
|
|
entities = []
|
2021-09-13 06:22:46 +00:00
|
|
|
for channel in cntrl.get_all("switch"):
|
|
|
|
entities.append(VelbusSwitch(channel))
|
2019-07-29 07:21:26 +00:00
|
|
|
async_add_entities(entities)
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class VelbusSwitch(VelbusEntity, SwitchEntity):
|
2017-07-26 12:03:29 +00:00
|
|
|
"""Representation of a switch."""
|
|
|
|
|
2021-11-11 12:46:35 +00:00
|
|
|
_channel: VelbusRelay
|
|
|
|
|
2017-07-26 12:03:29 +00:00
|
|
|
@property
|
2021-09-13 06:22:46 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-07-26 12:03:29 +00:00
|
|
|
"""Return true if the switch is on."""
|
2021-09-13 06:22:46 +00:00
|
|
|
return self._channel.is_on()
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2021-09-13 06:22:46 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2017-07-26 12:03:29 +00:00
|
|
|
"""Instruct the switch to turn on."""
|
2021-09-13 06:22:46 +00:00
|
|
|
await self._channel.turn_on()
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2021-09-13 06:22:46 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2017-07-26 12:03:29 +00:00
|
|
|
"""Instruct the switch to turn off."""
|
2021-09-13 06:22:46 +00:00
|
|
|
await self._channel.turn_off()
|