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
|
2021-03-11 19:41:01 +01:00
|
|
|
from typing import Any, Callable
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2020-04-26 18:50:37 +02:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-03-06 00:37:56 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
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-11 19:41:01 +01:00
|
|
|
from . import VerisureDataUpdateCoordinator
|
|
|
|
from .const import CONF_SMARTPLUGS, DOMAIN
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2021-03-06 00:37:56 +01:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: dict[str, Any],
|
|
|
|
add_entities: Callable[[list[Entity], bool], None],
|
|
|
|
discovery_info: dict[str, Any] | None = None,
|
2021-03-11 19:41:01 +01:00
|
|
|
) -> None:
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the Verisure switch platform."""
|
2021-03-11 19:41:01 +01:00
|
|
|
coordinator = hass.data[DOMAIN]
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2021-03-11 19:41:01 +01:00
|
|
|
if not int(coordinator.config.get(CONF_SMARTPLUGS, 1)):
|
|
|
|
return
|
2021-03-06 00:37:56 +01:00
|
|
|
|
2021-03-11 19:41:01 +01:00
|
|
|
add_entities(
|
|
|
|
[
|
|
|
|
VerisureSmartplug(coordinator, device_label)
|
|
|
|
for device_label in coordinator.get("$.smartPlugs[*].deviceLabel")
|
|
|
|
]
|
|
|
|
)
|
2015-08-12 13:00:47 +02:00
|
|
|
|
|
|
|
|
2021-03-11 19:41:01 +01:00
|
|
|
class VerisureSmartplug(CoordinatorEntity, SwitchEntity):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of a Verisure smartplug."""
|
|
|
|
|
2021-03-11 19:41:01 +01:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: VerisureDataUpdateCoordinator, device_id: str
|
|
|
|
) -> None:
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the Verisure device."""
|
2021-03-11 19:41:01 +01:00
|
|
|
super().__init__(coordinator)
|
2017-06-26 22:30:25 +02:00
|
|
|
self._device_label = device_id
|
|
|
|
self._change_timestamp = 0
|
|
|
|
self._state = False
|
2015-08-12 13:00:47 +02:00
|
|
|
|
|
|
|
@property
|
2021-03-06 00:37:56 +01:00
|
|
|
def name(self) -> str:
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the name or location of the smartplug."""
|
2021-03-11 19:41:01 +01:00
|
|
|
return self.coordinator.get_first(
|
2019-07-31 12:25:30 -07:00
|
|
|
"$.smartPlugs[?(@.deviceLabel == '%s')].area", self._device_label
|
|
|
|
)
|
2015-08-12 13:00:47 +02: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-11 19:41:01 +01:00
|
|
|
self.coordinator.get_first(
|
2019-07-31 12:25:30 -07:00
|
|
|
"$.smartPlugs[?(@.deviceLabel == '%s')].currentState",
|
|
|
|
self._device_label,
|
|
|
|
)
|
|
|
|
== "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-11 19:41:01 +01:00
|
|
|
self.coordinator.get_first(
|
|
|
|
"$.smartPlugs[?(@.deviceLabel == '%s')]", self._device_label
|
|
|
|
)
|
2019-07-31 12:25:30 -07:00
|
|
|
is not None
|
|
|
|
)
|
2016-05-04 03:53:11 +02:00
|
|
|
|
2021-03-06 00:37:56 +01:00
|
|
|
def turn_on(self, **kwargs) -> None:
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Set smartplug status on."""
|
2021-03-11 19:41:01 +01:00
|
|
|
self.coordinator.session.set_smartplug_state(self._device_label, True)
|
2017-06-26 22:30:25 +02:00
|
|
|
self._state = True
|
2020-02-13 23:57:07 +02:00
|
|
|
self._change_timestamp = monotonic()
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2021-03-06 00:37:56 +01:00
|
|
|
def turn_off(self, **kwargs) -> None:
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Set smartplug status off."""
|
2021-03-11 19:41:01 +01:00
|
|
|
self.coordinator.session.set_smartplug_state(self._device_label, False)
|
2017-06-26 22:30:25 +02:00
|
|
|
self._state = False
|
2020-02-13 23:57:07 +02:00
|
|
|
self._change_timestamp = monotonic()
|