2020-08-24 10:43:31 +00:00
|
|
|
"""Switch for Shelly."""
|
2021-07-21 17:11:44 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any, cast
|
|
|
|
|
2021-09-10 21:48:55 +00:00
|
|
|
from aioshelly.block_device import Block
|
2020-08-28 15:33:34 +00:00
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
from . import ShellyDeviceWrapper
|
2020-11-11 19:13:14 +00:00
|
|
|
from .const import COAP, DATA_CONFIG_ENTRY, DOMAIN
|
2020-09-07 12:13:20 +00:00
|
|
|
from .entity import ShellyBlockEntity
|
2020-11-19 10:42:24 +00:00
|
|
|
from .utils import async_remove_shelly_entity
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-08-24 10:43:31 +00:00
|
|
|
"""Set up switches for device."""
|
2020-11-11 19:13:14 +00:00
|
|
|
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][COAP]
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2020-09-10 10:08:17 +00:00
|
|
|
# In roller mode the relay blocks exist but do not contain required info
|
2020-09-15 14:29:24 +00:00
|
|
|
if (
|
|
|
|
wrapper.model in ["SHSW-21", "SHSW-25"]
|
|
|
|
and wrapper.device.settings["mode"] != "relay"
|
|
|
|
):
|
2020-09-10 10:08:17 +00:00
|
|
|
return
|
|
|
|
|
2020-11-05 17:38:53 +00:00
|
|
|
relay_blocks = []
|
|
|
|
for block in wrapper.device.blocks:
|
2020-11-12 09:12:56 +00:00
|
|
|
if block.type == "relay":
|
|
|
|
appliance_type = wrapper.device.settings["relays"][int(block.channel)].get(
|
|
|
|
"appliance_type"
|
2020-11-05 17:38:53 +00:00
|
|
|
)
|
2020-11-12 09:12:56 +00:00
|
|
|
if not appliance_type or appliance_type.lower() != "light":
|
|
|
|
relay_blocks.append(block)
|
|
|
|
unique_id = (
|
|
|
|
f'{wrapper.device.shelly["mac"]}-{block.type}_{block.channel}'
|
|
|
|
)
|
2020-11-19 10:42:24 +00:00
|
|
|
await async_remove_shelly_entity(
|
2020-11-12 09:12:56 +00:00
|
|
|
hass,
|
|
|
|
"light",
|
|
|
|
unique_id,
|
|
|
|
)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
if not relay_blocks:
|
2020-08-24 10:43:31 +00:00
|
|
|
return
|
|
|
|
|
2020-09-06 14:05:10 +00:00
|
|
|
async_add_entities(RelaySwitch(wrapper, block) for block in relay_blocks)
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RelaySwitch(ShellyBlockEntity, SwitchEntity):
|
|
|
|
"""Switch that controls a relay block on Shelly devices."""
|
|
|
|
|
2020-09-10 21:07:23 +00:00
|
|
|
def __init__(self, wrapper: ShellyDeviceWrapper, block: Block) -> None:
|
2020-08-24 10:43:31 +00:00
|
|
|
"""Initialize relay switch."""
|
2020-08-28 15:33:34 +00:00
|
|
|
super().__init__(wrapper, block)
|
2021-07-21 17:11:44 +00:00
|
|
|
self.control_result: dict[str, Any] | None = None
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""If switch is on."""
|
|
|
|
if self.control_result:
|
2021-07-21 17:11:44 +00:00
|
|
|
return cast(bool, self.control_result["ison"])
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
return bool(self.block.output)
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-08-24 10:43:31 +00:00
|
|
|
"""Turn on relay."""
|
2021-05-04 16:10:28 +00:00
|
|
|
self.control_result = await self.set_state(turn="on")
|
2020-08-24 10:43:31 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-08-24 10:43:31 +00:00
|
|
|
"""Turn off relay."""
|
2021-05-04 16:10:28 +00:00
|
|
|
self.control_result = await self.set_state(turn="off")
|
2020-08-24 10:43:31 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@callback
|
2021-07-21 17:11:44 +00:00
|
|
|
def _update_callback(self) -> None:
|
2020-08-24 10:43:31 +00:00
|
|
|
"""When device updates, clear control result that overrides state."""
|
|
|
|
self.control_result = None
|
|
|
|
super()._update_callback()
|