Improve type hints in xiaomi_miio number entity (#76466)

This commit is contained in:
epenet
2022-08-10 16:25:03 +02:00
committed by GitHub
parent acaa20cabe
commit eeff766078

View File

@@ -4,6 +4,8 @@ from __future__ import annotations
import dataclasses
from dataclasses import dataclass
from miio import Device
from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.components.number.const import DOMAIN as PLATFORM_DOMAIN
from homeassistant.config_entries import ConfigEntry
@@ -12,6 +14,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import (
CONF_DEVICE,
@@ -91,11 +94,17 @@ ATTR_VOLUME = "volume"
@dataclass
class XiaomiMiioNumberDescription(NumberEntityDescription):
class XiaomiMiioNumberMixin:
"""A class that describes number entities."""
method: str
@dataclass
class XiaomiMiioNumberDescription(NumberEntityDescription, XiaomiMiioNumberMixin):
"""A class that describes number entities."""
available_with_device_off: bool = True
method: str | None = None
@dataclass
@@ -325,7 +334,16 @@ async def async_setup_entry(
class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
"""Representation of a generic Xiaomi attribute selector."""
def __init__(self, device, entry, unique_id, coordinator, description):
entity_description: XiaomiMiioNumberDescription
def __init__(
self,
device: Device,
entry: ConfigEntry,
unique_id: str,
coordinator: DataUpdateCoordinator,
description: XiaomiMiioNumberDescription,
) -> None:
"""Initialize the generic Xiaomi attribute selector."""
super().__init__(device, entry, unique_id, coordinator)
@@ -335,7 +353,7 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
self.entity_description = description
@property
def available(self):
def available(self) -> bool:
"""Return the number controller availability."""
if (
super().available
@@ -345,7 +363,7 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
return False
return super().available
async def async_set_native_value(self, value):
async def async_set_native_value(self, value: float) -> None:
"""Set an option of the miio device."""
method = getattr(self, self.entity_description.method)
if await method(int(value)):
@@ -353,7 +371,7 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
self.async_write_ha_state()
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self) -> None:
"""Fetch state from the device."""
# On state change the device doesn't provide the new state immediately.
self._attr_native_value = self._extract_value_from_attribute(
@@ -407,7 +425,7 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
delay_off_countdown * 60,
)
async def async_set_led_brightness_level(self, level: int):
async def async_set_led_brightness_level(self, level: int) -> bool:
"""Set the led brightness level."""
return await self._try_command(
"Setting the led brightness level of the miio device failed.",
@@ -415,7 +433,7 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
level,
)
async def async_set_led_brightness(self, level: int):
async def async_set_led_brightness(self, level: int) -> bool:
"""Set the led brightness level."""
return await self._try_command(
"Setting the led brightness level of the miio device failed.",
@@ -423,7 +441,7 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
level,
)
async def async_set_favorite_rpm(self, rpm: int):
async def async_set_favorite_rpm(self, rpm: int) -> bool:
"""Set the target motor speed."""
return await self._try_command(
"Setting the favorite rpm of the miio device failed.",