2021-05-03 10:52:22 +00:00
|
|
|
"""Support for AVM FRITZ!SmartHome switch devices."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2023-11-20 16:13:52 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2024-06-26 19:45:17 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2023-11-20 16:13:52 +00:00
|
|
|
from . import FritzBoxDeviceEntity
|
2024-06-26 19:45:17 +00:00
|
|
|
from .const import DOMAIN
|
2024-05-01 18:51:39 +00:00
|
|
|
from .coordinator import FritzboxConfigEntry
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
async def async_setup_entry(
|
2024-05-01 18:51:39 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: FritzboxConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-04-25 00:40:12 +00:00
|
|
|
) -> None:
|
2021-05-03 10:52:22 +00:00
|
|
|
"""Set up the FRITZ!SmartHome switch from ConfigEntry."""
|
2024-05-01 18:51:39 +00:00
|
|
|
coordinator = entry.runtime_data
|
2023-11-20 16:13:52 +00:00
|
|
|
|
|
|
|
@callback
|
2023-12-13 17:07:29 +00:00
|
|
|
def _add_entities(devices: set[str] | None = None) -> None:
|
2023-11-20 16:13:52 +00:00
|
|
|
"""Add devices."""
|
2023-12-13 17:07:29 +00:00
|
|
|
if devices is None:
|
|
|
|
devices = coordinator.new_devices
|
|
|
|
if not devices:
|
2023-11-20 16:13:52 +00:00
|
|
|
return
|
|
|
|
async_add_entities(
|
2023-11-20 18:02:02 +00:00
|
|
|
FritzboxSwitch(coordinator, ain)
|
2023-12-13 17:07:29 +00:00
|
|
|
for ain in devices
|
2023-11-20 18:02:02 +00:00
|
|
|
if coordinator.data.devices[ain].has_switch
|
2023-11-20 16:13:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
entry.async_on_unload(coordinator.async_add_listener(_add_entities))
|
|
|
|
|
2023-12-16 18:38:58 +00:00
|
|
|
_add_entities(set(coordinator.data.devices))
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
|
2022-11-18 16:37:56 +00:00
|
|
|
class FritzboxSwitch(FritzBoxDeviceEntity, SwitchEntity):
|
2021-05-03 10:52:22 +00:00
|
|
|
"""The switch class for FRITZ!SmartHome switches."""
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-15 05:54:11 +00:00
|
|
|
def is_on(self) -> bool:
|
2018-04-17 10:40:36 +00:00
|
|
|
"""Return true if the switch is on."""
|
2022-11-19 14:12:24 +00:00
|
|
|
return self.data.switch_state # type: ignore [no-any-return]
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-04-17 10:40:36 +00:00
|
|
|
"""Turn the switch on."""
|
2024-06-26 19:45:17 +00:00
|
|
|
self.check_lock_state()
|
2022-11-19 14:12:24 +00:00
|
|
|
await self.hass.async_add_executor_job(self.data.set_switch_state_on)
|
2021-04-25 00:40:12 +00:00
|
|
|
await self.coordinator.async_refresh()
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-04-17 10:40:36 +00:00
|
|
|
"""Turn the switch off."""
|
2024-06-26 19:45:17 +00:00
|
|
|
self.check_lock_state()
|
2022-11-19 14:12:24 +00:00
|
|
|
await self.hass.async_add_executor_job(self.data.set_switch_state_off)
|
2021-04-25 00:40:12 +00:00
|
|
|
await self.coordinator.async_refresh()
|
2024-06-26 19:45:17 +00:00
|
|
|
|
|
|
|
def check_lock_state(self) -> None:
|
|
|
|
"""Raise an Error if manual switching via FRITZ!Box user interface is disabled."""
|
|
|
|
if self.data.lock:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="manual_switching_disabled",
|
|
|
|
)
|