2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Verisure sensors."""
|
2021-03-05 23:37:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
2020-09-05 19:09:14 +00:00
|
|
|
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS
|
2021-03-05 23:37:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-03-11 18:41:01 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2015-08-11 07:28:07 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
from .const import CONF_HYDROMETERS, CONF_MOUSE, CONF_THERMOMETERS, DOMAIN
|
2021-03-14 09:38:09 +00:00
|
|
|
from .coordinator import VerisureDataUpdateCoordinator
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2015-08-12 11:00:47 +00:00
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: dict[str, Any],
|
2021-03-14 09:38:09 +00:00
|
|
|
add_entities: Callable[[list[CoordinatorEntity], bool], None],
|
2021-03-05 23:37:56 +00:00
|
|
|
discovery_info: dict[str, Any] | None = None,
|
|
|
|
) -> None:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Verisure platform."""
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator = hass.data[DOMAIN]
|
2015-08-11 07:28:07 +00:00
|
|
|
|
2021-03-14 09:38:09 +00:00
|
|
|
sensors: list[CoordinatorEntity] = []
|
2021-03-11 18:41:01 +00:00
|
|
|
if int(coordinator.config.get(CONF_THERMOMETERS, 1)):
|
2019-07-31 19:25:30 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
2021-03-14 09:38:09 +00:00
|
|
|
VerisureThermometer(coordinator, serial_number)
|
|
|
|
for serial_number, values in coordinator.data["climate"].items()
|
|
|
|
if "temperature" in values
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2016-02-27 20:50:19 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
if int(coordinator.config.get(CONF_HYDROMETERS, 1)):
|
2019-07-31 19:25:30 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
2021-03-14 09:38:09 +00:00
|
|
|
VerisureHygrometer(coordinator, serial_number)
|
|
|
|
for serial_number, values in coordinator.data["climate"].items()
|
|
|
|
if "humidity" in values
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2016-02-27 20:50:19 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
if int(coordinator.config.get(CONF_MOUSE, 1)):
|
2019-07-31 19:25:30 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
2021-03-14 09:38:09 +00:00
|
|
|
VerisureMouseDetection(coordinator, serial_number)
|
|
|
|
for serial_number in coordinator.data["mice"]
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2016-02-12 19:52:02 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors)
|
2015-08-11 07:28:07 +00:00
|
|
|
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureThermometer(CoordinatorEntity, Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Verisure thermometer."""
|
2015-08-11 07:28:07 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
2021-03-14 09:38:09 +00:00
|
|
|
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
|
2021-03-11 18:41:01 +00:00
|
|
|
) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-03-11 18:41:01 +00:00
|
|
|
super().__init__(coordinator)
|
2021-03-14 09:38:09 +00:00
|
|
|
self.serial_number = serial_number
|
2015-08-12 11:00:47 +00:00
|
|
|
|
2015-08-11 07:28:07 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2021-03-14 09:38:09 +00:00
|
|
|
"""Return the name of the entity."""
|
|
|
|
name = self.coordinator.data["climate"][self.serial_number]["deviceArea"]
|
|
|
|
return f"{name} Temperature"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this entity."""
|
|
|
|
return f"{self.serial_number}_temperature"
|
2015-08-11 07:28:07 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def state(self) -> str | None:
|
2021-03-14 09:38:09 +00:00
|
|
|
"""Return the state of the entity."""
|
|
|
|
return self.coordinator.data["climate"][self.serial_number]["temperature"]
|
2015-08-12 11:00:47 +00:00
|
|
|
|
2016-05-04 01:53:11 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def available(self) -> bool:
|
2016-05-04 01:53:11 +00:00
|
|
|
"""Return True if entity is available."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
2021-03-14 09:38:09 +00:00
|
|
|
super().available
|
|
|
|
and self.serial_number in self.coordinator.data["climate"]
|
|
|
|
and "temperature" in self.coordinator.data["climate"][self.serial_number]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-05-04 01:53:11 +00:00
|
|
|
|
2015-08-12 11:00:47 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def unit_of_measurement(self) -> str:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity."""
|
2016-04-20 03:30:44 +00:00
|
|
|
return TEMP_CELSIUS
|
2015-08-12 11:00:47 +00:00
|
|
|
|
2015-08-17 11:05:49 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureHygrometer(CoordinatorEntity, Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Verisure hygrometer."""
|
2015-08-17 11:05:49 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
2021-03-14 09:38:09 +00:00
|
|
|
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
|
2021-03-11 18:41:01 +00:00
|
|
|
) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-03-11 18:41:01 +00:00
|
|
|
super().__init__(coordinator)
|
2021-03-14 09:38:09 +00:00
|
|
|
self.serial_number = serial_number
|
2015-08-17 11:05:49 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2021-03-14 09:38:09 +00:00
|
|
|
"""Return the name of the entity."""
|
|
|
|
name = self.coordinator.data["climate"][self.serial_number]["deviceArea"]
|
|
|
|
return f"{name} Humidity"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this entity."""
|
|
|
|
return f"{self.serial_number}_humidity"
|
2015-08-17 11:05:49 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def state(self) -> str | None:
|
2021-03-14 09:38:09 +00:00
|
|
|
"""Return the state of the entity."""
|
|
|
|
return self.coordinator.data["climate"][self.serial_number]["humidity"]
|
2015-08-17 11:05:49 +00:00
|
|
|
|
2016-05-04 01:53:11 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def available(self) -> bool:
|
2016-05-04 01:53:11 +00:00
|
|
|
"""Return True if entity is available."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
2021-03-14 09:38:09 +00:00
|
|
|
super().available
|
|
|
|
and self.serial_number in self.coordinator.data["climate"]
|
|
|
|
and "humidity" in self.coordinator.data["climate"][self.serial_number]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-05-04 01:53:11 +00:00
|
|
|
|
2015-08-17 11:05:49 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def unit_of_measurement(self) -> str:
|
2017-06-26 20:30:25 +00:00
|
|
|
"""Return the unit of measurement of this entity."""
|
2020-09-05 19:09:14 +00:00
|
|
|
return PERCENTAGE
|
2015-08-17 11:05:49 +00:00
|
|
|
|
2016-02-12 19:52:02 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureMouseDetection(CoordinatorEntity, Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Verisure mouse detector."""
|
2016-02-12 19:52:02 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
2021-03-14 09:38:09 +00:00
|
|
|
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
|
2021-03-11 18:41:01 +00:00
|
|
|
) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-03-11 18:41:01 +00:00
|
|
|
super().__init__(coordinator)
|
2021-03-14 09:38:09 +00:00
|
|
|
self.serial_number = serial_number
|
2016-02-12 19:52:02 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2021-03-14 09:38:09 +00:00
|
|
|
"""Return the name of the entity."""
|
|
|
|
name = self.coordinator.data["mice"][self.serial_number]["area"]
|
|
|
|
return f"{name} Mouse"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this entity."""
|
|
|
|
return f"{self.serial_number}_mice"
|
2016-02-12 19:52:02 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def state(self) -> str | None:
|
2017-06-26 20:30:25 +00:00
|
|
|
"""Return the state of the device."""
|
2021-03-14 09:38:09 +00:00
|
|
|
return self.coordinator.data["mice"][self.serial_number]["detections"]
|
2016-02-12 19:52:02 +00:00
|
|
|
|
2016-05-04 01:53:11 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def available(self) -> bool:
|
2016-05-04 01:53:11 +00:00
|
|
|
"""Return True if entity is available."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
2021-03-14 09:38:09 +00:00
|
|
|
super().available
|
|
|
|
and self.serial_number in self.coordinator.data["mice"]
|
|
|
|
and "detections" in self.coordinator.data["mice"][self.serial_number]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-05-04 01:53:11 +00:00
|
|
|
|
2016-02-12 19:52:02 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def unit_of_measurement(self) -> str:
|
2017-06-26 20:30:25 +00:00
|
|
|
"""Return the unit of measurement of this entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "Mice"
|