diff --git a/homeassistant/components/tradfri/sensor.py b/homeassistant/components/tradfri/sensor.py index 7f0ed233d1b..1e7d771cb39 100644 --- a/homeassistant/components/tradfri/sensor.py +++ b/homeassistant/components/tradfri/sensor.py @@ -1,13 +1,25 @@ """Support for IKEA Tradfri sensors.""" +from __future__ import annotations + +from typing import Any, Callable, cast + +from pytradfri.command import Command from homeassistant.components.sensor import SensorEntity +from homeassistant.config_entries import ConfigEntry from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE +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 -async def async_setup_entry(hass, config_entry, async_add_entities): +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] @@ -32,12 +44,16 @@ class TradfriSensor(TradfriBaseDevice, SensorEntity): _attr_device_class = DEVICE_CLASS_BATTERY _attr_native_unit_of_measurement = PERCENTAGE - def __init__(self, device, api, gateway_id): + def __init__( + self, device: Command, api: Callable[[str], Any], gateway_id: str + ) -> None: """Initialize the device.""" super().__init__(device, api, gateway_id) self._attr_unique_id = f"{gateway_id}-{device.id}" @property - def native_value(self): + def native_value(self) -> int | None: """Return the current state of the device.""" - return self._device.device_info.battery_level + if not self._device: + return None + return cast(int, self._device.device_info.battery_level)