2021-07-13 11:59:34 +00:00
|
|
|
"""Support for Freedompro sensor."""
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
SensorEntity,
|
|
|
|
)
|
2021-07-15 07:27:31 +00:00
|
|
|
from homeassistant.const import LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS
|
2021-07-13 11:59:34 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
DEVICE_CLASS_MAP = {
|
|
|
|
"temperatureSensor": DEVICE_CLASS_TEMPERATURE,
|
|
|
|
"humiditySensor": DEVICE_CLASS_HUMIDITY,
|
|
|
|
"lightSensor": DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
}
|
|
|
|
STATE_CLASS_MAP = {
|
|
|
|
"temperatureSensor": STATE_CLASS_MEASUREMENT,
|
|
|
|
"humiditySensor": STATE_CLASS_MEASUREMENT,
|
|
|
|
"lightSensor": None,
|
|
|
|
}
|
|
|
|
UNIT_MAP = {
|
|
|
|
"temperatureSensor": TEMP_CELSIUS,
|
|
|
|
"humiditySensor": PERCENTAGE,
|
|
|
|
"lightSensor": LIGHT_LUX,
|
|
|
|
}
|
|
|
|
DEVICE_KEY_MAP = {
|
|
|
|
"temperatureSensor": "currentTemperature",
|
|
|
|
"humiditySensor": "currentRelativeHumidity",
|
|
|
|
"lightSensor": "currentAmbientLightLevel",
|
|
|
|
}
|
|
|
|
SUPPORTED_SENSORS = {"temperatureSensor", "humiditySensor", "lightSensor"}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up Freedompro sensor."""
|
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities(
|
2021-07-15 07:27:31 +00:00
|
|
|
Device(device, coordinator)
|
2021-07-13 11:59:34 +00:00
|
|
|
for device in coordinator.data
|
|
|
|
if device["type"] in SUPPORTED_SENSORS
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Device(CoordinatorEntity, SensorEntity):
|
|
|
|
"""Representation of an Freedompro sensor."""
|
|
|
|
|
2021-07-15 07:27:31 +00:00
|
|
|
def __init__(self, device, coordinator):
|
2021-07-13 11:59:34 +00:00
|
|
|
"""Initialize the Freedompro sensor."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_name = device["name"]
|
|
|
|
self._attr_unique_id = device["uid"]
|
|
|
|
self._type = device["type"]
|
|
|
|
self._attr_device_info = {
|
|
|
|
"name": self.name,
|
|
|
|
"identifiers": {
|
|
|
|
(DOMAIN, self.unique_id),
|
|
|
|
},
|
2021-07-15 07:27:31 +00:00
|
|
|
"model": device["type"],
|
2021-07-13 11:59:34 +00:00
|
|
|
"manufacturer": "Freedompro",
|
|
|
|
}
|
2021-07-15 07:27:31 +00:00
|
|
|
self._attr_device_class = DEVICE_CLASS_MAP[device["type"]]
|
|
|
|
self._attr_state_class = STATE_CLASS_MAP[device["type"]]
|
2021-08-12 12:23:56 +00:00
|
|
|
self._attr_native_unit_of_measurement = UNIT_MAP[device["type"]]
|
|
|
|
self._attr_native_value = 0
|
2021-07-13 11:59:34 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
device = next(
|
|
|
|
(
|
|
|
|
device
|
|
|
|
for device in self.coordinator.data
|
|
|
|
if device["uid"] == self.unique_id
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
if device is not None and "state" in device:
|
|
|
|
state = device["state"]
|
2021-08-12 12:23:56 +00:00
|
|
|
self._attr_native_value = state[DEVICE_KEY_MAP[self._type]]
|
2021-07-13 11:59:34 +00:00
|
|
|
super()._handle_coordinator_update()
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self._handle_coordinator_update()
|