core/homeassistant/components/verisure/binary_sensor.py

134 lines
4.1 KiB
Python
Raw Normal View History

"""Support for Verisure binary sensors."""
from __future__ import annotations
from collections.abc import Iterable
from typing import Any, Callable
2019-11-13 13:12:36 +00:00
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY,
DEVICE_CLASS_OPENING,
BinarySensorEntity,
2019-11-13 13:12:36 +00:00
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import CONF_GIID, DOMAIN
from .coordinator import VerisureDataUpdateCoordinator
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: Callable[[Iterable[Entity]], None],
) -> None:
"""Set up Verisure binary sensors based on a config entry."""
coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
sensors: list[Entity] = [VerisureEthernetStatus(coordinator)]
sensors.extend(
VerisureDoorWindowSensor(coordinator, serial_number)
for serial_number in coordinator.data["door_window"]
)
2019-11-13 13:12:36 +00:00
async_add_entities(sensors)
class VerisureDoorWindowSensor(CoordinatorEntity, BinarySensorEntity):
"""Representation of a Verisure door window sensor."""
coordinator: VerisureDataUpdateCoordinator
def __init__(
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
) -> None:
"""Initialize the Verisure door window sensor."""
super().__init__(coordinator)
self.serial_number = serial_number
@property
def name(self) -> str:
"""Return the name of this entity."""
return self.coordinator.data["door_window"][self.serial_number]["area"]
@property
def unique_id(self) -> str:
"""Return the unique ID for this entity."""
return f"{self.serial_number}_door_window"
@property
def device_info(self) -> dict[str, Any]:
"""Return device information about this entity."""
area = self.coordinator.data["door_window"][self.serial_number]["area"]
return {
"name": area,
"suggested_area": area,
"manufacturer": "Verisure",
"model": "Shock Sensor Detector",
"identifiers": {(DOMAIN, self.serial_number)},
"via_device": (DOMAIN, self.coordinator.entry.data[CONF_GIID]),
}
@property
def device_class(self) -> str:
"""Return the class of this entity."""
return DEVICE_CLASS_OPENING
@property
def is_on(self) -> bool:
"""Return the state of the sensor."""
2019-07-31 19:25:30 +00:00
return (
self.coordinator.data["door_window"][self.serial_number]["state"] == "OPEN"
2019-07-31 19:25:30 +00:00
)
@property
def available(self) -> bool:
"""Return True if entity is available."""
2019-07-31 19:25:30 +00:00
return (
super().available
and self.serial_number in self.coordinator.data["door_window"]
2019-07-31 19:25:30 +00:00
)
2019-11-13 13:12:36 +00:00
class VerisureEthernetStatus(CoordinatorEntity, BinarySensorEntity):
2019-11-13 13:12:36 +00:00
"""Representation of a Verisure VBOX internet status."""
coordinator: VerisureDataUpdateCoordinator
2019-11-13 13:12:36 +00:00
@property
def name(self) -> str:
"""Return the name of this entity."""
2019-11-13 13:12:36 +00:00
return "Verisure Ethernet status"
@property
def unique_id(self) -> str:
"""Return the unique ID for this entity."""
return f"{self.coordinator.entry.data[CONF_GIID]}_ethernet"
@property
def device_info(self) -> dict[str, Any]:
"""Return device information about this entity."""
return {
"name": "Verisure Alarm",
"manufacturer": "Verisure",
"model": "VBox",
"identifiers": {(DOMAIN, self.coordinator.entry.data[CONF_GIID])},
}
2019-11-13 13:12:36 +00:00
@property
def is_on(self) -> bool:
2019-11-13 13:12:36 +00:00
"""Return the state of the sensor."""
return self.coordinator.data["ethernet"]
2019-11-13 13:12:36 +00:00
@property
def available(self) -> bool:
2019-11-13 13:12:36 +00:00
"""Return True if entity is available."""
return super().available and self.coordinator.data["ethernet"] is not None
2019-11-13 13:12:36 +00:00
@property
def device_class(self) -> str:
"""Return the class of this entity."""
2019-11-13 13:12:36 +00:00
return DEVICE_CLASS_CONNECTIVITY