2021-06-09 11:22:37 +00:00
|
|
|
"""Support for Ambee sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-06-10 12:18:09 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
ATTR_STATE_CLASS,
|
|
|
|
DOMAIN as SENSOR_DOMAIN,
|
|
|
|
SensorEntity,
|
|
|
|
)
|
2021-06-09 11:22:37 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_DEVICE_CLASS,
|
2021-06-10 12:18:09 +00:00
|
|
|
ATTR_ICON,
|
2021-06-09 11:22:37 +00:00
|
|
|
ATTR_IDENTIFIERS,
|
|
|
|
ATTR_MANUFACTURER,
|
|
|
|
ATTR_NAME,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import StateType
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
|
|
|
|
2021-06-10 12:18:09 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_ENABLED_BY_DEFAULT,
|
|
|
|
ATTR_ENTRY_TYPE,
|
|
|
|
DOMAIN,
|
|
|
|
ENTRY_TYPE_SERVICE,
|
|
|
|
SENSORS,
|
|
|
|
SERVICES,
|
|
|
|
)
|
|
|
|
from .models import AmbeeSensor
|
2021-06-09 11:22:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-06-10 12:18:09 +00:00
|
|
|
"""Set up Ambee sensors based on a config entry."""
|
2021-06-09 11:22:37 +00:00
|
|
|
async_add_entities(
|
2021-06-10 12:18:09 +00:00
|
|
|
AmbeeSensorEntity(
|
|
|
|
coordinator=hass.data[DOMAIN][entry.entry_id][service_key],
|
|
|
|
entry_id=entry.entry_id,
|
|
|
|
sensor_key=sensor_key,
|
|
|
|
sensor=sensor,
|
|
|
|
service_key=service_key,
|
|
|
|
service=SERVICES[service_key],
|
|
|
|
)
|
|
|
|
for service_key, service_sensors in SENSORS.items()
|
|
|
|
for sensor_key, sensor in service_sensors.items()
|
2021-06-09 11:22:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-06-10 12:18:09 +00:00
|
|
|
class AmbeeSensorEntity(CoordinatorEntity, SensorEntity):
|
2021-06-09 11:22:37 +00:00
|
|
|
"""Defines an Ambee sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
2021-06-10 12:18:09 +00:00
|
|
|
self,
|
|
|
|
*,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
entry_id: str,
|
|
|
|
sensor_key: str,
|
|
|
|
sensor: AmbeeSensor,
|
|
|
|
service_key: str,
|
|
|
|
service: str,
|
2021-06-09 11:22:37 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize Ambee sensor."""
|
|
|
|
super().__init__(coordinator=coordinator)
|
2021-06-10 12:18:09 +00:00
|
|
|
self._sensor_key = sensor_key
|
|
|
|
self._service_key = service_key
|
|
|
|
|
|
|
|
self.entity_id = f"{SENSOR_DOMAIN}.{service_key}_{sensor_key}"
|
|
|
|
self._attr_device_class = sensor.get(ATTR_DEVICE_CLASS)
|
|
|
|
self._attr_entity_registry_enabled_default = sensor.get(
|
|
|
|
ATTR_ENABLED_BY_DEFAULT, True
|
|
|
|
)
|
|
|
|
self._attr_icon = sensor.get(ATTR_ICON)
|
|
|
|
self._attr_name = sensor.get(ATTR_NAME)
|
|
|
|
self._attr_state_class = sensor.get(ATTR_STATE_CLASS)
|
|
|
|
self._attr_unique_id = f"{entry_id}_{service_key}_{sensor_key}"
|
|
|
|
self._attr_unit_of_measurement = sensor.get(ATTR_UNIT_OF_MEASUREMENT)
|
2021-06-09 11:22:37 +00:00
|
|
|
|
2021-06-10 12:18:09 +00:00
|
|
|
self._attr_device_info = {
|
|
|
|
ATTR_IDENTIFIERS: {(DOMAIN, f"{entry_id}_{service_key}")},
|
|
|
|
ATTR_NAME: service,
|
|
|
|
ATTR_MANUFACTURER: "Ambee",
|
|
|
|
ATTR_ENTRY_TYPE: ENTRY_TYPE_SERVICE,
|
|
|
|
}
|
2021-06-09 11:22:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> StateType:
|
|
|
|
"""Return the state of the sensor."""
|
2021-06-10 12:18:09 +00:00
|
|
|
value = getattr(self.coordinator.data, self._sensor_key)
|
|
|
|
if isinstance(value, str):
|
|
|
|
return value.lower()
|
|
|
|
return value # type: ignore[no-any-return]
|