Skip to content

Commit

Permalink
chore: some more typing (#44)
Browse files Browse the repository at this point in the history
* chore: some more typing

* chore: isort

---------

Co-authored-by: Necroneco <caibinqing@live.com>
  • Loading branch information
chemelli74 and caibinqing authored May 31, 2024
1 parent cbeed99 commit 1e1f0bb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
55 changes: 33 additions & 22 deletions custom_components/midea_ac_lan/light.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Any
from typing import Any, cast

from homeassistant.components.light import (
ATTR_BRIGHTNESS,
Expand All @@ -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

Expand All @@ -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
):
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
25 changes: 18 additions & 7 deletions custom_components/midea_ac_lan/lock.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
36 changes: 20 additions & 16 deletions custom_components/midea_ac_lan/midea_entity.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"])[
Expand All @@ -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']} "
Expand All @@ -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()
Expand Down

0 comments on commit 1e1f0bb

Please sign in to comment.