Strictly type sensor.py (#56388)

pull/56406/head
jan iversen 2021-09-18 21:49:47 +02:00 committed by GitHub
parent 3ce8109e5e
commit 5c19368ce3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 4 deletions

View File

@ -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)