2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Verisure binary sensors."""
|
2021-03-05 23:37:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
2019-11-13 13:12:36 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_CONNECTIVITY,
|
2020-04-23 19:57:07 +00:00
|
|
|
BinarySensorEntity,
|
2019-11-13 13:12:36 +00:00
|
|
|
)
|
2021-03-05 23:37:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-03-11 18:41:01 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
from . import CONF_DOOR_WINDOW, DOMAIN, VerisureDataUpdateCoordinator
|
2017-06-26 20:30:25 +00:00
|
|
|
|
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: dict[str, Any],
|
|
|
|
add_entities: Callable[[list[Entity], bool], None],
|
|
|
|
discovery_info: dict[str, Any] | None = None,
|
|
|
|
) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the Verisure binary sensors."""
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator = hass.data[DOMAIN]
|
2017-06-26 20:30:25 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
sensors = [VerisureEthernetStatus(coordinator)]
|
|
|
|
|
|
|
|
if int(coordinator.config.get(CONF_DOOR_WINDOW, 1)):
|
2019-07-31 19:25:30 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
2021-03-11 18:41:01 +00:00
|
|
|
VerisureDoorWindowSensor(coordinator, device_label)
|
|
|
|
for device_label in coordinator.get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"$.doorWindow.doorWindowDevice[*].deviceLabel"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2019-11-13 13:12:36 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors)
|
2017-06-26 20:30:25 +00:00
|
|
|
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureDoorWindowSensor(CoordinatorEntity, BinarySensorEntity):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Representation of a Verisure door window sensor."""
|
2017-06-26 20:30:25 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: VerisureDataUpdateCoordinator, device_label: str
|
|
|
|
) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize the Verisure door window sensor."""
|
2021-03-11 18:41:01 +00:00
|
|
|
super().__init__(coordinator)
|
2017-06-26 20:30:25 +00:00
|
|
|
self._device_label = device_label
|
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2017-06-26 20:30:25 +00:00
|
|
|
"""Return the name of the binary sensor."""
|
2021-03-11 18:41:01 +00:00
|
|
|
return self.coordinator.get_first(
|
2017-06-26 20:30:25 +00:00
|
|
|
"$.doorWindow.doorWindowDevice[?(@.deviceLabel=='%s')].area",
|
2019-07-31 19:25:30 +00:00
|
|
|
self._device_label,
|
|
|
|
)
|
2017-06-26 20:30:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-06-26 20:30:25 +00:00
|
|
|
"""Return the state of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
2021-03-11 18:41:01 +00:00
|
|
|
self.coordinator.get_first(
|
2019-07-31 19:25:30 +00:00
|
|
|
"$.doorWindow.doorWindowDevice[?(@.deviceLabel=='%s')].state",
|
|
|
|
self._device_label,
|
|
|
|
)
|
|
|
|
== "OPEN"
|
|
|
|
)
|
2017-06-26 20:30:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def available(self) -> bool:
|
2017-06-26 20:30:25 +00:00
|
|
|
"""Return True if entity is available."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
2021-03-11 18:41:01 +00:00
|
|
|
self.coordinator.get_first(
|
2019-07-31 19:25:30 +00:00
|
|
|
"$.doorWindow.doorWindowDevice[?(@.deviceLabel=='%s')]",
|
|
|
|
self._device_label,
|
|
|
|
)
|
|
|
|
is not None
|
|
|
|
)
|
2017-06-26 20:30:25 +00:00
|
|
|
|
2019-11-13 13:12:36 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureEthernetStatus(CoordinatorEntity, BinarySensorEntity):
|
2019-11-13 13:12:36 +00:00
|
|
|
"""Representation of a Verisure VBOX internet status."""
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
2019-11-13 13:12:36 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2019-11-13 13:12:36 +00:00
|
|
|
"""Return the name of the binary sensor."""
|
|
|
|
return "Verisure Ethernet status"
|
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def is_on(self) -> bool:
|
2019-11-13 13:12:36 +00:00
|
|
|
"""Return the state of the sensor."""
|
2021-03-11 18:41:01 +00:00
|
|
|
return self.coordinator.get_first("$.ethernetConnectedNow")
|
2019-11-13 13:12:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def available(self) -> bool:
|
2019-11-13 13:12:36 +00:00
|
|
|
"""Return True if entity is available."""
|
2021-03-11 18:41:01 +00:00
|
|
|
return self.coordinator.get_first("$.ethernetConnectedNow") is not None
|
2019-11-13 13:12:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def device_class(self) -> str:
|
2019-11-13 13:12:36 +00:00
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
|
|
return DEVICE_CLASS_CONNECTIVITY
|