From 1e1f0bb2e621c0c9f59d20a743b80b2322548355 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 31 May 2024 14:22:48 +0900 Subject: [PATCH] chore: some more typing (#44) * chore: some more typing * chore: isort --------- Co-authored-by: Necroneco --- custom_components/midea_ac_lan/light.py | 55 +++++++++++-------- custom_components/midea_ac_lan/lock.py | 25 ++++++--- .../midea_ac_lan/midea_entity.py | 36 ++++++------ 3 files changed, 71 insertions(+), 45 deletions(-) diff --git a/custom_components/midea_ac_lan/light.py b/custom_components/midea_ac_lan/light.py index a3768a8c..0fde9b18 100644 --- a/custom_components/midea_ac_lan/light.py +++ b/custom_components/midea_ac_lan/light.py @@ -1,5 +1,5 @@ import logging -from typing import Any +from typing import Any, cast from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -9,7 +9,10 @@ LightEntity, LightEntityFeature, ) +from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_DEVICE_ID, CONF_SWITCHES, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback from midealocal.devices.x13 import DeviceAttributes as X13Attributes from midealocal.devices.x13 import Midea13Device @@ -20,12 +23,18 @@ _LOGGER = logging.getLogger(__name__) -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: device_id = config_entry.data.get(CONF_DEVICE_ID) device = hass.data[DOMAIN][DEVICES].get(device_id) extra_switches = config_entry.options.get(CONF_SWITCHES, []) devs = [] - for entity_key, config in MIDEA_DEVICES[device.device_type]["entities"].items(): + for entity_key, config in cast( + dict, MIDEA_DEVICES[device.device_type]["entities"] + ).items(): if config["type"] == Platform.LIGHT and ( config.get("default") or entity_key in extra_switches ): @@ -64,7 +73,7 @@ class MideaLight(MideaEntity, LightEntity): _device: Midea13Device - def __init__(self, device: Midea13Device, entity_key: str): + def __init__(self, device: Midea13Device, entity_key: str) -> None: super().__init__(device, entity_key) self._attr_supported_features = _calc_supported_features(device) self._attr_supported_color_modes = _calc_supported_color_modes(device) @@ -83,24 +92,26 @@ def _calc_color_mode(self, supported: set[ColorMode]) -> ColorMode: return ColorMode.UNKNOWN @property - def is_on(self): - return self._device.get_attribute(X13Attributes.power) + def is_on(self) -> bool: + return cast(bool, self._device.get_attribute(X13Attributes.power)) @property - def brightness(self): - return self._device.get_attribute(X13Attributes.brightness) + def brightness(self) -> int | None: + return cast(int, self._device.get_attribute(X13Attributes.brightness)) @property - def rgb_color(self): - return self._device.get_attribute(X13Attributes.rgb_color) + def rgb_color(self) -> tuple[int, int, int] | None: + return cast(tuple, self._device.get_attribute(X13Attributes.rgb_color)) @property - def color_temp(self): + def color_temp(self) -> int | None: + if not self.color_temp_kelvin: + return None return round(1000000 / self.color_temp_kelvin) @property - def color_temp_kelvin(self): - return self._device.get_attribute(X13Attributes.color_temperature) + def color_temp_kelvin(self) -> int | None: + return cast(int, self._device.get_attribute(X13Attributes.color_temperature)) @property def min_mireds(self) -> int: @@ -112,21 +123,21 @@ def max_mireds(self) -> int: @property def min_color_temp_kelvin(self) -> int: - return self._device.color_temp_range[0] + return cast(int, self._device.color_temp_range[0]) @property def max_color_temp_kelvin(self) -> int: - return self._device.color_temp_range[1] + return cast(int, self._device.color_temp_range[1]) @property - def effect_list(self): - return self._device.effects + def effect_list(self) -> list[str] | None: + return cast(list, self._device.effects) @property - def effect(self): - return self._device.get_attribute(X13Attributes.effect) + def effect(self) -> str | None: + return cast(str, self._device.get_attribute(X13Attributes.effect)) - def turn_on(self, **kwargs: Any): + def turn_on(self, **kwargs: Any) -> None: if not self.is_on: self._device.set_attribute(attr=X13Attributes.power, value=True) for key in kwargs: @@ -141,10 +152,10 @@ def turn_on(self, **kwargs: Any): if key == ATTR_EFFECT: self._device.set_attribute(attr=X13Attributes.effect, value=value) - def turn_off(self): + def turn_off(self, **kwargs: Any) -> None: self._device.set_attribute(attr=X13Attributes.power, value=False) - def update_state(self, status): + def update_state(self, status: Any) -> None: try: self.schedule_update_ha_state() except Exception as e: diff --git a/custom_components/midea_ac_lan/lock.py b/custom_components/midea_ac_lan/lock.py index 28813fb9..b42caf65 100644 --- a/custom_components/midea_ac_lan/lock.py +++ b/custom_components/midea_ac_lan/lock.py @@ -1,17 +1,28 @@ +from typing import Any, cast + from homeassistant.components.lock import LockEntity +from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_DEVICE_ID, CONF_SWITCHES, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DEVICES, DOMAIN from .midea_devices import MIDEA_DEVICES from .midea_entity import MideaEntity -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: device_id = config_entry.data.get(CONF_DEVICE_ID) device = hass.data[DOMAIN][DEVICES].get(device_id) extra_switches = config_entry.options.get(CONF_SWITCHES, []) locks = [] - for entity_key, config in MIDEA_DEVICES[device.device_type]["entities"].items(): + for entity_key, config in cast( + dict, MIDEA_DEVICES[device.device_type]["entities"] + ).items(): if config["type"] == Platform.LOCK and entity_key in extra_switches: dev = MideaLock(device, entity_key) locks.append(dev) @@ -20,14 +31,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class MideaLock(MideaEntity, LockEntity): @property - def is_locked(self): - return self._device.get_attribute(self._entity_key) + def is_locked(self) -> bool: + return cast(bool, self._device.get_attribute(self._entity_key)) - def lock(self, **kwargs) -> None: + def lock(self, **kwargs: Any) -> None: self._device.set_attribute(attr=self._entity_key, value=True) - def unlock(self, **kwargs) -> None: + def unlock(self, **kwargs: Any) -> None: self._device.set_attribute(attr=self._entity_key, value=False) - def open(self, **kwargs) -> None: + def open(self, **kwargs: Any) -> None: self.unlock() diff --git a/custom_components/midea_ac_lan/midea_entity.py b/custom_components/midea_ac_lan/midea_entity.py index 3438d08c..8eec334e 100644 --- a/custom_components/midea_ac_lan/midea_entity.py +++ b/custom_components/midea_ac_lan/midea_entity.py @@ -1,6 +1,7 @@ import logging -from typing import cast +from typing import Any, cast +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import Entity from .const import DOMAIN @@ -10,7 +11,7 @@ class MideaEntity(Entity): - def __init__(self, device, entity_key: str): + def __init__(self, device: Any, entity_key: str) -> None: self._device = device self._device.register_update(self.update_state) self._config = cast(dict, MIDEA_DEVICES[self._device.device_type]["entities"])[ @@ -22,11 +23,11 @@ def __init__(self, device, entity_key: str): self._device_name = self._device.name @property - def device(self): + def device(self) -> Any: return self._device @property - def device_info(self): + def device_info(self) -> DeviceInfo: return { "manufacturer": "Midea", "model": f"{MIDEA_DEVICES[self._device.device_type]['name']} " @@ -37,30 +38,33 @@ def device_info(self): } @property - def unique_id(self): + def unique_id(self) -> str: return self._unique_id @property - def should_poll(self): + def should_poll(self) -> bool: return False @property - def name(self): - return ( - f"{self._device_name} {self._config.get('name')}" - if "name" in self._config - else self._device_name + def name(self) -> str: + return cast( + str, + ( + f"{self._device_name} {self._config.get('name')}" + if "name" in self._config + else self._device_name + ), ) @property - def available(self): - return self._device.available + def available(self) -> bool: + return bool(self._device.available) @property - def icon(self): - return self._config.get("icon") + def icon(self) -> str: + return cast(str, self._config.get("icon")) - def update_state(self, status): + def update_state(self, status: Any) -> None: if self._entity_key in status or "available" in status: try: self.schedule_update_ha_state()