core/homeassistant/components/verisure/sensor.py

171 lines
5.3 KiB
Python
Raw Normal View History

"""Support for Verisure sensors."""
from __future__ import annotations
from typing import Callable, Iterable
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant
2016-02-19 05:27:50 +00:00
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2015-08-11 07:28:07 +00:00
from .const import DOMAIN
from .coordinator import VerisureDataUpdateCoordinator
2015-08-12 11:00:47 +00:00
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: Callable[[Iterable[Entity]], None],
) -> None:
"""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()
if "temperature" in values
]
sensors.extend(
VerisureHygrometer(coordinator, serial_number)
for serial_number, values in coordinator.data["climate"].items()
if "humidity" in values
)
sensors.extend(
VerisureMouseDetection(coordinator, serial_number)
for serial_number in coordinator.data["mice"]
)
async_add_entities(sensors)
2015-08-11 07:28:07 +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
coordinator: VerisureDataUpdateCoordinator
def __init__(
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
) -> None:
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
super().__init__(coordinator)
self.serial_number = serial_number
2015-08-12 11:00:47 +00:00
2015-08-11 07:28:07 +00:00
@property
def name(self) -> str:
"""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
def state(self) -> str | None:
"""Return the state of the entity."""
return self.coordinator.data["climate"][self.serial_number]["temperature"]
2015-08-12 11:00:47 +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["climate"]
and "temperature" in self.coordinator.data["climate"][self.serial_number]
2019-07-31 19:25:30 +00:00
)
2015-08-12 11:00:47 +00:00
@property
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
class VerisureHygrometer(CoordinatorEntity, Entity):
2016-03-08 15:46:34 +00:00
"""Representation of a Verisure hygrometer."""
coordinator: VerisureDataUpdateCoordinator
def __init__(
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
) -> None:
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
super().__init__(coordinator)
self.serial_number = serial_number
@property
def name(self) -> str:
"""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"
@property
def state(self) -> str | None:
"""Return the state of the entity."""
return self.coordinator.data["climate"][self.serial_number]["humidity"]
@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["climate"]
and "humidity" in self.coordinator.data["climate"][self.serial_number]
2019-07-31 19:25:30 +00:00
)
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity."""
return PERCENTAGE
class VerisureMouseDetection(CoordinatorEntity, Entity):
2016-03-08 15:46:34 +00:00
"""Representation of a Verisure mouse detector."""
coordinator: VerisureDataUpdateCoordinator
def __init__(
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
) -> None:
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
super().__init__(coordinator)
self.serial_number = serial_number
@property
def name(self) -> str:
"""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"
@property
def state(self) -> str | None:
"""Return the state of the device."""
return self.coordinator.data["mice"][self.serial_number]["detections"]
@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["mice"]
and "detections" in self.coordinator.data["mice"][self.serial_number]
2019-07-31 19:25:30 +00:00
)
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity."""
2019-07-31 19:25:30 +00:00
return "Mice"