2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Verisure Smartplugs."""
|
2021-03-06 00:37:56 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-02-13 23:57:07 +02:00
|
|
|
from time import monotonic
|
2022-09-06 13:59:05 +02:00
|
|
|
from typing import Any
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2020-04-26 18:50:37 +02:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-03-15 20:30:44 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-03-06 00:37:56 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 22:36:48 +01:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-03-11 19:41:01 +01:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2021-03-15 23:59:41 +01:00
|
|
|
from .const import CONF_GIID, DOMAIN
|
2021-03-14 10:38:09 +01:00
|
|
|
from .coordinator import VerisureDataUpdateCoordinator
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2021-03-15 20:30:44 +01:00
|
|
|
async def async_setup_entry(
|
2021-03-06 00:37:56 +01:00
|
|
|
hass: HomeAssistant,
|
2021-03-15 20:30:44 +01:00
|
|
|
entry: ConfigEntry,
|
2021-05-04 22:36:48 +01:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-11 19:41:01 +01:00
|
|
|
) -> None:
|
2021-03-15 20:30:44 +01:00
|
|
|
"""Set up Verisure alarm control panel from a config entry."""
|
|
|
|
coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities(
|
|
|
|
VerisureSmartplug(coordinator, serial_number)
|
|
|
|
for serial_number in coordinator.data["smart_plugs"]
|
2021-03-11 19:41:01 +01:00
|
|
|
)
|
2015-08-12 13:00:47 +02:00
|
|
|
|
|
|
|
|
2022-03-21 14:20:35 +01:00
|
|
|
class VerisureSmartplug(CoordinatorEntity[VerisureDataUpdateCoordinator], SwitchEntity):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of a Verisure smartplug."""
|
|
|
|
|
2022-07-11 18:14:17 +02:00
|
|
|
_attr_has_entity_name = True
|
2023-07-03 17:38:54 +02:00
|
|
|
_attr_name = None
|
2022-07-11 18:14:17 +02:00
|
|
|
|
2021-03-11 19:41:01 +01:00
|
|
|
def __init__(
|
2021-03-14 10:38:09 +01:00
|
|
|
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
|
2021-03-11 19:41:01 +01:00
|
|
|
) -> None:
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the Verisure device."""
|
2021-03-11 19:41:01 +01:00
|
|
|
super().__init__(coordinator)
|
2021-05-22 18:13:50 +02:00
|
|
|
self._attr_unique_id = serial_number
|
|
|
|
|
2021-03-14 10:38:09 +01:00
|
|
|
self.serial_number = serial_number
|
2022-01-19 22:56:31 +01:00
|
|
|
self._change_timestamp: float = 0
|
2017-06-26 22:30:25 +02:00
|
|
|
self._state = False
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2021-03-15 20:30:44 +01:00
|
|
|
@property
|
2021-05-02 00:37:19 +02:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2021-03-15 23:59:41 +01:00
|
|
|
"""Return device information about this entity."""
|
2023-03-26 19:32:25 +02:00
|
|
|
area = self.coordinator.data["smart_plugs"][self.serial_number]["device"][
|
|
|
|
"area"
|
|
|
|
]
|
2021-10-15 00:35:09 +02:00
|
|
|
return DeviceInfo(
|
|
|
|
name=area,
|
|
|
|
suggested_area=area,
|
|
|
|
manufacturer="Verisure",
|
|
|
|
model="SmartPlug",
|
|
|
|
identifiers={(DOMAIN, self.serial_number)},
|
|
|
|
via_device=(DOMAIN, self.coordinator.entry.data[CONF_GIID]),
|
|
|
|
configuration_url="https://mypages.verisure.com",
|
|
|
|
)
|
2021-03-15 23:59:41 +01:00
|
|
|
|
|
|
|
@property
|
2021-03-06 00:37:56 +01:00
|
|
|
def is_on(self) -> bool:
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return true if on."""
|
2020-02-13 23:57:07 +02:00
|
|
|
if monotonic() - self._change_timestamp < 10:
|
2017-06-26 22:30:25 +02:00
|
|
|
return self._state
|
2019-07-31 12:25:30 -07:00
|
|
|
self._state = (
|
2021-03-14 10:38:09 +01:00
|
|
|
self.coordinator.data["smart_plugs"][self.serial_number]["currentState"]
|
2019-07-31 12:25:30 -07:00
|
|
|
== "ON"
|
|
|
|
)
|
2017-06-26 22:30:25 +02:00
|
|
|
return self._state
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2016-05-04 03:53:11 +02:00
|
|
|
@property
|
2021-03-06 00:37:56 +01:00
|
|
|
def available(self) -> bool:
|
2016-05-04 03:53:11 +02:00
|
|
|
"""Return True if entity is available."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return (
|
2021-03-14 10:38:09 +01:00
|
|
|
super().available
|
|
|
|
and self.serial_number in self.coordinator.data["smart_plugs"]
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2016-05-04 03:53:11 +02:00
|
|
|
|
2023-03-26 19:32:25 +02:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the smartplug on."""
|
|
|
|
await self.async_set_plug_state(True)
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2023-03-26 19:32:25 +02:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the smartplug off."""
|
|
|
|
await self.async_set_plug_state(False)
|
|
|
|
|
|
|
|
async def async_set_plug_state(self, state: bool) -> None:
|
|
|
|
"""Set smartplug state."""
|
|
|
|
command: dict[
|
|
|
|
str, str | dict[str, str]
|
|
|
|
] = self.coordinator.verisure.set_smartplug(self.serial_number, state)
|
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
self.coordinator.verisure.request,
|
|
|
|
command,
|
|
|
|
)
|
|
|
|
self._state = state
|
2020-02-13 23:57:07 +02:00
|
|
|
self._change_timestamp = monotonic()
|
2023-03-26 19:32:25 +02:00
|
|
|
await self.coordinator.async_request_refresh()
|