2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Verisure sensors."""
|
2021-03-06 00:37:56 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-16 16:30:04 +01:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-01 10:24:58 +01:00
|
|
|
SensorDeviceClass,
|
2021-03-22 19:47:44 +01:00
|
|
|
SensorEntity,
|
2021-12-20 20:56:33 +01:00
|
|
|
SensorStateClass,
|
2021-03-16 16:30:04 +01:00
|
|
|
)
|
2021-03-15 20:30:44 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-20 18:50:39 +01:00
|
|
|
from homeassistant.const import PERCENTAGE, UnitOfTemperature
|
2021-03-06 00:37:56 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-02 00:37:19 +02:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
2021-05-04 22:36:48 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-03-11 19:41:01 +01:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2015-08-11 09:28:07 +02:00
|
|
|
|
2021-03-15 23:59:41 +01:00
|
|
|
from .const import CONF_GIID, DEVICE_TYPE_NAME, 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-06 00:37:56 +01:00
|
|
|
) -> None:
|
2021-03-15 20:30:44 +01:00
|
|
|
"""Set up Verisure sensors based on a config entry."""
|
|
|
|
coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
|
|
|
|
sensors: list[Entity] = [
|
|
|
|
VerisureThermometer(coordinator, serial_number)
|
|
|
|
for serial_number, values in coordinator.data["climate"].items()
|
2023-03-26 19:32:25 +02:00
|
|
|
if "temperatureValue" in values
|
2021-03-15 20:30:44 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
sensors.extend(
|
|
|
|
VerisureHygrometer(coordinator, serial_number)
|
|
|
|
for serial_number, values in coordinator.data["climate"].items()
|
2023-03-26 19:32:25 +02:00
|
|
|
if values.get("humidityEnabled")
|
2021-03-15 20:30:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(sensors)
|
2015-08-11 09:28:07 +02:00
|
|
|
|
|
|
|
|
2022-03-21 14:20:35 +01:00
|
|
|
class VerisureThermometer(
|
|
|
|
CoordinatorEntity[VerisureDataUpdateCoordinator], SensorEntity
|
|
|
|
):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a Verisure thermometer."""
|
2015-08-11 09:28:07 +02:00
|
|
|
|
2021-12-01 10:24:58 +01:00
|
|
|
_attr_device_class = SensorDeviceClass.TEMPERATURE
|
2022-07-11 18:14:17 +02:00
|
|
|
_attr_has_entity_name = True
|
2022-12-20 18:50:39 +01:00
|
|
|
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
2021-12-20 20:56:33 +01:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2021-05-22 18:13:50 +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 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2021-03-11 19:41:01 +01:00
|
|
|
super().__init__(coordinator)
|
2021-05-22 18:13:50 +02:00
|
|
|
self._attr_unique_id = f"{serial_number}_temperature"
|
2021-03-14 10:38:09 +01:00
|
|
|
self.serial_number = serial_number
|
2015-08-12 13:00:47 +02:00
|
|
|
|
2021-03-15 23:59:41 +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
|
|
|
device_type = self.coordinator.data["climate"][self.serial_number]["device"][
|
|
|
|
"gui"
|
|
|
|
]["label"]
|
|
|
|
area = self.coordinator.data["climate"][self.serial_number]["device"]["area"]
|
2021-10-15 00:35:09 +02:00
|
|
|
return DeviceInfo(
|
|
|
|
name=area,
|
|
|
|
suggested_area=area,
|
|
|
|
manufacturer="Verisure",
|
|
|
|
model=DEVICE_TYPE_NAME.get(device_type, device_type),
|
|
|
|
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
|
|
|
|
2015-08-11 09:28:07 +02:00
|
|
|
@property
|
2021-08-11 21:17:16 +02:00
|
|
|
def native_value(self) -> str | None:
|
2021-03-14 10:38:09 +01:00
|
|
|
"""Return the state of the entity."""
|
2023-03-26 19:32:25 +02:00
|
|
|
return self.coordinator.data["climate"][self.serial_number]["temperatureValue"]
|
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["climate"]
|
2023-03-26 19:32:25 +02:00
|
|
|
and "temperatureValue"
|
|
|
|
in self.coordinator.data["climate"][self.serial_number]
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2016-05-04 03:53:11 +02:00
|
|
|
|
2015-08-17 13:05:49 +02:00
|
|
|
|
2022-03-21 14:20:35 +01:00
|
|
|
class VerisureHygrometer(
|
|
|
|
CoordinatorEntity[VerisureDataUpdateCoordinator], SensorEntity
|
|
|
|
):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a Verisure hygrometer."""
|
2015-08-17 13:05:49 +02:00
|
|
|
|
2021-12-01 10:24:58 +01:00
|
|
|
_attr_device_class = SensorDeviceClass.HUMIDITY
|
2022-07-11 18:14:17 +02:00
|
|
|
_attr_has_entity_name = True
|
2021-08-11 21:17:16 +02:00
|
|
|
_attr_native_unit_of_measurement = PERCENTAGE
|
2021-12-20 20:56:33 +01:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2021-05-22 18:13:50 +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 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2021-03-11 19:41:01 +01:00
|
|
|
super().__init__(coordinator)
|
2021-05-22 18:13:50 +02:00
|
|
|
self._attr_unique_id = f"{serial_number}_humidity"
|
2021-03-14 10:38:09 +01:00
|
|
|
self.serial_number = serial_number
|
2015-08-17 13:05:49 +02:00
|
|
|
|
2021-03-15 23:59:41 +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
|
|
|
device_type = self.coordinator.data["climate"][self.serial_number]["device"][
|
|
|
|
"gui"
|
|
|
|
]["label"]
|
|
|
|
area = self.coordinator.data["climate"][self.serial_number]["device"]["area"]
|
2021-10-15 00:35:09 +02:00
|
|
|
return DeviceInfo(
|
|
|
|
name=area,
|
|
|
|
suggested_area=area,
|
|
|
|
manufacturer="Verisure",
|
|
|
|
model=DEVICE_TYPE_NAME.get(device_type, device_type),
|
|
|
|
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
|
|
|
|
2015-08-17 13:05:49 +02:00
|
|
|
@property
|
2021-08-11 21:17:16 +02:00
|
|
|
def native_value(self) -> str | None:
|
2021-03-14 10:38:09 +01:00
|
|
|
"""Return the state of the entity."""
|
2023-03-26 19:32:25 +02:00
|
|
|
return self.coordinator.data["climate"][self.serial_number]["humidityValue"]
|
2015-08-17 13:05:49 +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["climate"]
|
2023-03-26 19:32:25 +02:00
|
|
|
and "humidityValue" in self.coordinator.data["climate"][self.serial_number]
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|