2019-02-13 20:21:14 +00:00
|
|
|
"""Support for IKEA Tradfri sensors."""
|
2021-09-18 19:49:47 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 14:19:06 +00:00
|
|
|
from collections.abc import Callable
|
2022-01-29 22:55:05 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Any, cast
|
2021-09-18 19:49:47 +00:00
|
|
|
|
|
|
|
from pytradfri.command import Command
|
2022-01-29 22:55:05 +00:00
|
|
|
from pytradfri.device import Device
|
2019-09-22 21:01:32 +00:00
|
|
|
|
2022-01-29 22:55:05 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2022-02-08 01:21:22 +00:00
|
|
|
SensorStateClass,
|
2022-01-29 22:55:05 +00:00
|
|
|
)
|
2021-09-18 19:49:47 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-02-08 01:21:22 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
PERCENTAGE,
|
|
|
|
TIME_HOURS,
|
|
|
|
Platform,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers import entity_registry
|
2021-09-18 19:49:47 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-12-01 05:23:39 +00:00
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
from .base_class import TradfriBaseEntity
|
2022-02-08 01:21:22 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_GATEWAY_ID,
|
|
|
|
COORDINATOR,
|
|
|
|
COORDINATOR_LIST,
|
|
|
|
DOMAIN,
|
|
|
|
KEY_API,
|
2022-04-05 12:00:45 +00:00
|
|
|
LOGGER,
|
2022-02-08 01:21:22 +00:00
|
|
|
)
|
2022-01-27 10:12:52 +00:00
|
|
|
from .coordinator import TradfriDeviceDataUpdateCoordinator
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2022-01-29 22:55:05 +00:00
|
|
|
@dataclass
|
|
|
|
class TradfriSensorEntityDescriptionMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value: Callable[[Device], Any | None]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class TradfriSensorEntityDescription(
|
|
|
|
SensorEntityDescription,
|
|
|
|
TradfriSensorEntityDescriptionMixin,
|
|
|
|
):
|
|
|
|
"""Class describing Tradfri sensor entities."""
|
|
|
|
|
|
|
|
|
|
|
|
def _get_air_quality(device: Device) -> int | None:
|
|
|
|
"""Fetch the air quality value."""
|
2022-11-21 07:51:04 +00:00
|
|
|
assert device.air_purifier_control is not None
|
2022-01-29 22:55:05 +00:00
|
|
|
if (
|
|
|
|
device.air_purifier_control.air_purifiers[0].air_quality == 65535
|
|
|
|
): # The sensor returns 65535 if the fan is turned off
|
|
|
|
return None
|
|
|
|
|
|
|
|
return cast(int, device.air_purifier_control.air_purifiers[0].air_quality)
|
|
|
|
|
|
|
|
|
2022-02-08 01:21:22 +00:00
|
|
|
def _get_filter_time_left(device: Device) -> int:
|
|
|
|
"""Fetch the filter's remaining life (in hours)."""
|
2022-11-21 07:51:04 +00:00
|
|
|
assert device.air_purifier_control is not None
|
2022-02-08 01:21:22 +00:00
|
|
|
return round(
|
2022-11-21 07:51:04 +00:00
|
|
|
cast(
|
|
|
|
int, device.air_purifier_control.air_purifiers[0].filter_lifetime_remaining
|
|
|
|
)
|
|
|
|
/ 60
|
2022-02-08 01:21:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_DESCRIPTIONS_BATTERY: tuple[TradfriSensorEntityDescription, ...] = (
|
|
|
|
TradfriSensorEntityDescription(
|
|
|
|
key="battery_level",
|
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
value=lambda device: cast(int, device.device_info.battery_level),
|
|
|
|
),
|
2022-01-29 22:55:05 +00:00
|
|
|
)
|
|
|
|
|
2022-02-08 01:21:22 +00:00
|
|
|
|
|
|
|
SENSOR_DESCRIPTIONS_FAN: tuple[TradfriSensorEntityDescription, ...] = (
|
|
|
|
TradfriSensorEntityDescription(
|
|
|
|
key="aqi",
|
|
|
|
name="air quality",
|
|
|
|
device_class=SensorDeviceClass.AQI,
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
value=_get_air_quality,
|
|
|
|
),
|
|
|
|
TradfriSensorEntityDescription(
|
2022-04-05 12:00:45 +00:00
|
|
|
key="filter_life_remaining",
|
2022-02-08 01:21:22 +00:00
|
|
|
name="filter time left",
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
native_unit_of_measurement=TIME_HOURS,
|
|
|
|
icon="mdi:clock-outline",
|
|
|
|
value=_get_filter_time_left,
|
|
|
|
),
|
2022-01-29 22:55:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-08 01:21:22 +00:00
|
|
|
@callback
|
|
|
|
def _migrate_old_unique_ids(hass: HomeAssistant, old_unique_id: str, key: str) -> None:
|
|
|
|
"""Migrate unique IDs to the new format."""
|
|
|
|
ent_reg = entity_registry.async_get(hass)
|
|
|
|
|
|
|
|
entity_id = ent_reg.async_get_entity_id(Platform.SENSOR, DOMAIN, old_unique_id)
|
|
|
|
|
|
|
|
if entity_id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
new_unique_id = f"{old_unique_id}-{key}"
|
|
|
|
|
|
|
|
try:
|
|
|
|
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
|
|
|
|
except ValueError:
|
2022-04-05 12:00:45 +00:00
|
|
|
LOGGER.warning(
|
2022-02-08 01:21:22 +00:00
|
|
|
"Skip migration of id [%s] to [%s] because it already exists",
|
|
|
|
old_unique_id,
|
|
|
|
new_unique_id,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2022-04-05 12:00:45 +00:00
|
|
|
LOGGER.debug(
|
2022-02-08 01:21:22 +00:00
|
|
|
"Migrating unique_id from [%s] to [%s]",
|
|
|
|
old_unique_id,
|
|
|
|
new_unique_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-18 19:49:47 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-09-19 19:21:43 +00:00
|
|
|
"""Set up a Tradfri config entry."""
|
2019-10-06 17:24:56 +00:00
|
|
|
gateway_id = config_entry.data[CONF_GATEWAY_ID]
|
2022-01-27 10:12:52 +00:00
|
|
|
coordinator_data = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
|
|
|
|
api = coordinator_data[KEY_API]
|
2018-09-19 19:21:43 +00:00
|
|
|
|
2022-01-29 22:55:05 +00:00
|
|
|
entities: list[TradfriSensor] = []
|
|
|
|
|
|
|
|
for device_coordinator in coordinator_data[COORDINATOR_LIST]:
|
2021-11-09 14:18:13 +00:00
|
|
|
if (
|
2022-01-27 10:12:52 +00:00
|
|
|
not device_coordinator.device.has_light_control
|
|
|
|
and not device_coordinator.device.has_socket_control
|
|
|
|
and not device_coordinator.device.has_signal_repeater_control
|
|
|
|
and not device_coordinator.device.has_air_purifier_control
|
2022-01-29 22:55:05 +00:00
|
|
|
):
|
2022-02-08 01:21:22 +00:00
|
|
|
descriptions = SENSOR_DESCRIPTIONS_BATTERY
|
2022-01-29 22:55:05 +00:00
|
|
|
elif device_coordinator.device.has_air_purifier_control:
|
2022-02-08 01:21:22 +00:00
|
|
|
descriptions = SENSOR_DESCRIPTIONS_FAN
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for description in descriptions:
|
|
|
|
# Added in Home assistant 2022.3
|
|
|
|
_migrate_old_unique_ids(
|
|
|
|
hass=hass,
|
|
|
|
old_unique_id=f"{gateway_id}-{device_coordinator.device.id}",
|
|
|
|
key=description.key,
|
|
|
|
)
|
2022-01-29 22:55:05 +00:00
|
|
|
|
|
|
|
entities.append(
|
|
|
|
TradfriSensor(
|
|
|
|
device_coordinator,
|
|
|
|
api,
|
|
|
|
gateway_id,
|
|
|
|
description=description,
|
|
|
|
)
|
|
|
|
)
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2022-01-29 22:55:05 +00:00
|
|
|
async_add_entities(entities)
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2022-01-29 22:55:05 +00:00
|
|
|
|
|
|
|
class TradfriSensor(TradfriBaseEntity, SensorEntity):
|
2017-10-05 16:05:38 +00:00
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
2022-01-29 22:55:05 +00:00
|
|
|
entity_description: TradfriSensorEntityDescription
|
2021-05-31 08:50:11 +00:00
|
|
|
|
2021-09-18 19:49:47 +00:00
|
|
|
def __init__(
|
2021-09-20 12:33:50 +00:00
|
|
|
self,
|
2022-01-27 10:12:52 +00:00
|
|
|
device_coordinator: TradfriDeviceDataUpdateCoordinator,
|
2021-09-20 12:33:50 +00:00
|
|
|
api: Callable[[Command | list[Command]], Any],
|
|
|
|
gateway_id: str,
|
2022-01-29 22:55:05 +00:00
|
|
|
description: TradfriSensorEntityDescription,
|
2021-09-18 19:49:47 +00:00
|
|
|
) -> None:
|
2022-01-29 22:55:05 +00:00
|
|
|
"""Initialize a Tradfri sensor."""
|
2022-01-27 10:12:52 +00:00
|
|
|
super().__init__(
|
|
|
|
device_coordinator=device_coordinator,
|
|
|
|
api=api,
|
|
|
|
gateway_id=gateway_id,
|
|
|
|
)
|
|
|
|
|
2022-01-29 22:55:05 +00:00
|
|
|
self.entity_description = description
|
|
|
|
|
2022-02-08 01:21:22 +00:00
|
|
|
self._attr_unique_id = f"{self._attr_unique_id}-{description.key}"
|
|
|
|
|
|
|
|
if description.name:
|
|
|
|
self._attr_name = f"{self._attr_name}: {description.name}"
|
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
self._refresh() # Set initial state
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
def _refresh(self) -> None:
|
|
|
|
"""Refresh the device."""
|
2022-01-29 22:55:05 +00:00
|
|
|
self._attr_native_value = self.entity_description.value(self.coordinator.data)
|