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