2021-05-03 10:52:22 +00:00
|
|
|
"""Support for AVM FRITZ!SmartHome switch devices."""
|
2021-05-15 05:54:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2021-07-22 16:19:39 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-04-25 00:40:12 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-04-20 13:00:07 +00:00
|
|
|
from homeassistant.const import (
|
2021-04-25 00:40:12 +00:00
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_NAME,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
2020-04-20 13:00:07 +00:00
|
|
|
)
|
2021-04-25 00:40:12 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
from . import FritzBoxEntity
|
2020-04-20 13:00:07 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_STATE_DEVICE_LOCKED,
|
|
|
|
ATTR_STATE_LOCKED,
|
2021-04-25 00:40:12 +00:00
|
|
|
CONF_COORDINATOR,
|
2020-04-20 13:00:07 +00:00
|
|
|
DOMAIN as FRITZBOX_DOMAIN,
|
|
|
|
)
|
2021-05-15 05:54:11 +00:00
|
|
|
from .model import SwitchExtraAttributes
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-30 18:38:59 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, 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."""
|
2021-05-15 05:54:11 +00:00
|
|
|
entities: list[FritzboxSwitch] = []
|
2021-04-25 00:40:12 +00:00
|
|
|
coordinator = hass.data[FRITZBOX_DOMAIN][entry.entry_id][CONF_COORDINATOR]
|
|
|
|
|
|
|
|
for ain, device in coordinator.data.items():
|
|
|
|
if not device.has_switch:
|
|
|
|
continue
|
|
|
|
|
|
|
|
entities.append(
|
|
|
|
FritzboxSwitch(
|
|
|
|
{
|
|
|
|
ATTR_NAME: f"{device.name}",
|
|
|
|
ATTR_ENTITY_ID: f"{device.ain}",
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT: None,
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
2021-07-22 16:19:39 +00:00
|
|
|
ATTR_STATE_CLASS: None,
|
2021-04-25 00:40:12 +00:00
|
|
|
},
|
|
|
|
coordinator,
|
|
|
|
ain,
|
|
|
|
)
|
|
|
|
)
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
async_add_entities(entities)
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
class FritzboxSwitch(FritzBoxEntity, 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."""
|
2021-05-15 05:54:11 +00:00
|
|
|
return self.device.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."""
|
2021-04-25 00:40:12 +00:00
|
|
|
await self.hass.async_add_executor_job(self.device.set_switch_state_on)
|
|
|
|
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."""
|
2021-04-25 00:40:12 +00:00
|
|
|
await self.hass.async_add_executor_job(self.device.set_switch_state_off)
|
|
|
|
await self.coordinator.async_refresh()
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-15 05:54:11 +00:00
|
|
|
def extra_state_attributes(self) -> SwitchExtraAttributes:
|
2018-04-17 10:40:36 +00:00
|
|
|
"""Return the state attributes of the device."""
|
2021-05-15 05:54:11 +00:00
|
|
|
attrs: SwitchExtraAttributes = {
|
|
|
|
ATTR_STATE_DEVICE_LOCKED: self.device.device_lock,
|
|
|
|
ATTR_STATE_LOCKED: self.device.lock,
|
|
|
|
}
|
2018-04-17 10:40:36 +00:00
|
|
|
return attrs
|