From e804cd251a67c22480c7940c0330e9581061e68b Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Sat, 1 Jun 2024 23:06:14 +0900 Subject: [PATCH] chore: typing fan (#48) * chore: typing fan * chore: isort --- custom_components/midea_ac_lan/fan.py | 136 +++++++++++++------------- 1 file changed, 67 insertions(+), 69 deletions(-) diff --git a/custom_components/midea_ac_lan/fan.py b/custom_components/midea_ac_lan/fan.py index 9bca84d0..2be30572 100644 --- a/custom_components/midea_ac_lan/fan.py +++ b/custom_components/midea_ac_lan/fan.py @@ -1,14 +1,11 @@ import logging -from typing import Any +from typing import Any, cast from homeassistant.components.fan import FanEntity, FanEntityFeature -from homeassistant.const import ( - CONF_DEVICE_ID, - CONF_SWITCHES, - STATE_OFF, - STATE_ON, - Platform, -) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_DEVICE_ID, CONF_SWITCHES, STATE_ON, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback from midealocal.devices.ac import DeviceAttributes as ACAttributes from midealocal.devices.ce import DeviceAttributes as CEAttributes from midealocal.devices.x40 import DeviceAttributes as X40Attributes @@ -20,12 +17,20 @@ _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(): + devs: list[ + MideaFAFan | MideaB6Fan | MideaACFreshAirFan | MideaCEFan | Midea40Fan + ] = [] + for entity_key, config in cast( + dict, MIDEA_DEVICES[device.device_type]["entities"] + ).items(): if config["type"] == Platform.FAN and ( config.get("default") or entity_key in extra_switches ): @@ -43,7 +48,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class MideaFan(MideaEntity, FanEntity): - def __init__(self, device, entity_key): + def __init__(self, device: Any, entity_key: str) -> None: super().__init__(device, entity_key) def turn_on( @@ -59,55 +64,53 @@ def turn_on( self._device.turn_on(fan_speed=fan_speed, mode=preset_mode) @property - def preset_modes(self): + def preset_modes(self) -> list[str] | None: return ( self._device.preset_modes if hasattr(self._device, "preset_modes") else None ) @property def is_on(self) -> bool: - return self._device.get_attribute("power") + return cast(bool, self._device.get_attribute("power")) @property - def oscillating(self): - return self._device.get_attribute("oscillate") + def oscillating(self) -> bool: + return cast(bool, self._device.get_attribute("oscillate")) @property - def preset_mode(self): - return self._device.get_attribute("mode") + def preset_mode(self) -> str | None: + return cast(str, self._device.get_attribute("mode")) @property - def fan_speed(self): - return self._device.get_attribute("fan_speed") + def fan_speed(self) -> int | None: + return cast(int, self._device.get_attribute("fan_speed")) - def turn_off(self): + def turn_off(self, **kwargs: Any) -> None: self._device.set_attribute(attr="power", value=False) - def toggle(self): - toggle = not self.is_on - self._device.set_attribute(attr="power", value=toggle) - - def oscillate(self, oscillating: bool): + def oscillate(self, oscillating: bool) -> None: self._device.set_attribute(attr="oscillate", value=oscillating) - def set_preset_mode(self, preset_mode: str): + def set_preset_mode(self, preset_mode: str) -> None: self._device.set_attribute(attr="mode", value=preset_mode.capitalize()) @property - def percentage(self): + def percentage(self) -> int | None: + if not self.fan_speed: + return None return round(self.fan_speed * self.percentage_step) - def set_percentage(self, percentage: int): + def set_percentage(self, percentage: int) -> None: fan_speed = round(percentage / self.percentage_step) self._device.set_attribute(attr="fan_speed", value=fan_speed) - async def async_set_percentage(self, percentage: int): + async def async_set_percentage(self, percentage: int) -> None: if percentage == 0: await self.async_turn_off() else: await self.hass.async_add_executor_job(self.set_percentage, percentage) - def update_state(self, status): + def update_state(self, status: Any) -> None: try: self.schedule_update_ha_state() except Exception as e: @@ -120,7 +123,7 @@ def update_state(self, status): class MideaFAFan(MideaFan): - def __init__(self, device, entity_key): + def __init__(self, device: Any, entity_key: str) -> None: super().__init__(device, entity_key) self._attr_supported_features = ( FanEntityFeature.SET_SPEED @@ -131,7 +134,7 @@ def __init__(self, device, entity_key): class MideaB6Fan(MideaFan): - def __init__(self, device, entity_key): + def __init__(self, device: Any, entity_key: str) -> None: super().__init__(device, entity_key) self._attr_supported_features = ( FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE @@ -148,83 +151,78 @@ def __init__(self, device, entity_key): self._attr_speed_count = 100 @property - def preset_modes(self): - return self._device.fresh_air_fan_speeds - - @property - def state(self): - return ( - STATE_ON - if self._device.get_attribute(ACAttributes.fresh_air_power) - else STATE_OFF - ) + def preset_modes(self) -> list[str] | None: + return cast(list, self._device.fresh_air_fan_speeds) @property def is_on(self) -> bool: return self.state == STATE_ON @property - def fan_speed(self): - return self._device.get_attribute(ACAttributes.fresh_air_fan_speed) + def fan_speed(self) -> int: + return cast(int, self._device.get_attribute(ACAttributes.fresh_air_fan_speed)) - def turn_on(self, percentage, preset_mode, **kwargs): + def turn_on( + self, + percentage: int | None = None, + preset_mode: str | None = None, + **kwargs: Any, + ) -> None: self._device.set_attribute(attr=ACAttributes.fresh_air_power, value=True) - def turn_off(self): + def turn_off(self, **kwargs: Any) -> None: self._device.set_attribute(attr=ACAttributes.fresh_air_power, value=False) - def toggle(self): - toggle = not self.is_on - self._device.set_attribute(attr=ACAttributes.fresh_air_power, value=toggle) - - def set_percentage(self, percentage: int): + def set_percentage(self, percentage: int) -> None: fan_speed = int(percentage / self.percentage_step + 0.5) self._device.set_attribute( attr=ACAttributes.fresh_air_fan_speed, value=fan_speed, ) - def set_preset_mode(self, preset_mode: str): + def set_preset_mode(self, preset_mode: str) -> None: self._device.set_attribute(attr=ACAttributes.fresh_air_mode, value=preset_mode) @property - def preset_mode(self): - return self._device.get_attribute(attr=ACAttributes.fresh_air_mode) + def preset_mode(self) -> str | None: + return cast(str, self._device.get_attribute(attr=ACAttributes.fresh_air_mode)) class MideaCEFan(MideaFan): - def __init__(self, device, entity_key): + def __init__(self, device: Any, entity_key: str) -> None: super().__init__(device, entity_key) self._attr_supported_features = ( FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE ) self._attr_speed_count = self._device.speed_count - def turn_on(self, percentage, preset_mode, **kwargs): + def turn_on( + self, + percentage: int | None = None, + preset_mode: str | None = None, + **kwargs: Any, + ) -> None: self._device.set_attribute(attr=CEAttributes.power, value=True) - async def async_set_percentage(self, percentage: int): + async def async_set_percentage(self, percentage: int) -> None: await self.hass.async_add_executor_job(self.set_percentage, percentage) class Midea40Fan(MideaFan): - def __init__(self, device, entity_key): + def __init__(self, device: Any, entity_key: str) -> None: super().__init__(device, entity_key) self._attr_supported_features = ( FanEntityFeature.SET_SPEED | FanEntityFeature.OSCILLATE ) self._attr_speed_count = 2 - @property - def state(self): - return ( - STATE_ON - if self._device.get_attribute(attr=X40Attributes.fan_speed) > 0 - else STATE_OFF - ) - - def turn_on(self, percentage, preset_mode, **kwargs): + def turn_on( + self, + percentage: int | None = None, + preset_mode: str | None = None, + **kwargs: Any, + ) -> None: self._device.set_attribute(attr=X40Attributes.fan_speed, value=1) - def turn_off(self): + def turn_off(self, **kwargs: Any) -> None: self._device.set_attribute(attr=X40Attributes.fan_speed, value=0)