core/homeassistant/components/tradfri/sensor.py

65 lines
2.0 KiB
Python
Raw Normal View History

"""Support for IKEA Tradfri sensors."""
2021-09-18 19:49:47 +00:00
from __future__ import annotations
from collections.abc import Callable
from typing import Any, cast
2021-09-18 19:49:47 +00:00
from pytradfri.command import Command
from homeassistant.components.sensor import SensorEntity
2021-09-18 19:49:47 +00:00
from homeassistant.config_entries import ConfigEntry
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
from .base_class import TradfriBaseDevice
from .const import CONF_GATEWAY_ID, DEVICES, DOMAIN, KEY_API
2021-09-18 19:49:47 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Tradfri config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID]
tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
api = tradfri_data[KEY_API]
devices = tradfri_data[DEVICES]
async_add_entities(
TradfriSensor(dev, api, gateway_id)
for dev in devices
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
)
class TradfriSensor(TradfriBaseDevice, SensorEntity):
"""The platform class required by Home Assistant."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_native_unit_of_measurement = PERCENTAGE
2021-09-18 19:49:47 +00:00
def __init__(
self,
device: Command,
api: Callable[[Command | list[Command]], Any],
gateway_id: str,
2021-09-18 19:49:47 +00:00
) -> None:
"""Initialize the device."""
super().__init__(device, api, gateway_id)
self._attr_unique_id = f"{gateway_id}-{device.id}"
@property
2021-09-18 19:49:47 +00:00
def native_value(self) -> int | None:
"""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)