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
|
|
|
|
from typing import Any, cast
|
2021-09-18 19:49:47 +00:00
|
|
|
|
|
|
|
from pytradfri.command import Command
|
2019-09-22 21:01:32 +00:00
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-09-18 19:49:47 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-09-05 19:09:14 +00:00
|
|
|
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
|
2021-09-18 19:49:47 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-12-01 05:23:39 +00:00
|
|
|
|
2019-10-09 19:56:16 +00:00
|
|
|
from .base_class import TradfriBaseDevice
|
2020-09-05 21:02:32 +00:00
|
|
|
from .const import CONF_GATEWAY_ID, DEVICES, DOMAIN, KEY_API
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-10-05 16:05:38 +00:00
|
|
|
|
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]
|
2020-09-03 16:39:24 +00:00
|
|
|
tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
api = tradfri_data[KEY_API]
|
2020-09-05 21:02:32 +00:00
|
|
|
devices = tradfri_data[DEVICES]
|
2018-09-19 19:21:43 +00:00
|
|
|
|
2021-11-09 14:18:13 +00:00
|
|
|
async_add_entities(
|
|
|
|
TradfriSensor(dev, api, gateway_id)
|
2020-09-05 21:02:32 +00:00
|
|
|
for dev in devices
|
2021-11-09 14:18:13 +00:00
|
|
|
if (
|
|
|
|
not dev.has_light_control
|
|
|
|
and not dev.has_socket_control
|
|
|
|
and not dev.has_blind_control
|
|
|
|
and not dev.has_signal_repeater_control
|
|
|
|
and not dev.has_air_purifier_control
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-10-05 16:05:38 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class TradfriSensor(TradfriBaseDevice, SensorEntity):
|
2017-10-05 16:05:38 +00:00
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
2021-05-31 08:50:11 +00:00
|
|
|
_attr_device_class = DEVICE_CLASS_BATTERY
|
2021-08-11 16:57:50 +00:00
|
|
|
_attr_native_unit_of_measurement = PERCENTAGE
|
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,
|
|
|
|
device: Command,
|
|
|
|
api: Callable[[Command | list[Command]], Any],
|
|
|
|
gateway_id: str,
|
2021-09-18 19:49:47 +00:00
|
|
|
) -> None:
|
2017-10-05 16:05:38 +00:00
|
|
|
"""Initialize the device."""
|
2019-10-06 17:24:56 +00:00
|
|
|
super().__init__(device, api, gateway_id)
|
2021-09-06 06:49:00 +00:00
|
|
|
self._attr_unique_id = f"{gateway_id}-{device.id}"
|
2017-10-05 16:05:38 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-18 19:49:47 +00:00
|
|
|
def native_value(self) -> int | None:
|
2017-10-05 16:05:38 +00:00
|
|
|
"""Return the current state of the device."""
|
2021-09-18 19:49:47 +00:00
|
|
|
if not self._device:
|
|
|
|
return None
|
|
|
|
return cast(int, self._device.device_info.battery_level)
|