Skip to content

Commit

Permalink
chore: more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 committed May 30, 2024
1 parent 5411738 commit 1265537
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 59 deletions.
85 changes: 52 additions & 33 deletions custom_components/midea_ac_lan/number.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,88 @@
from typing import Any, cast
from homeassistant.components.number import NumberEntity
from homeassistant.const import CONF_DEVICE_ID, CONF_SWITCHES, Platform

from .const import DEVICES, DOMAIN
from .midea_devices import MIDEA_DEVICES
from .midea_entity import MideaEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback


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, [])
numbers = []
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.NUMBER and entity_key in extra_switches:
dev = MideaNumber(device, entity_key)
numbers.append(dev)
async_add_entities(numbers)


class MideaNumber(MideaEntity, NumberEntity):
def __init__(self, device, entity_key: str):
def __init__(self, device: Any, entity_key: str) -> None:
super().__init__(device, entity_key)
self._max_value = self._config.get("max")
self._min_value = self._config.get("min")
self._step_value = self._config.get("step")

@property
def native_min_value(self):
return (
self._min_value
if isinstance(self._min_value, int)
else (
self._device.get_attribute(attr=self._min_value)
if self._device.get_attribute(attr=self._min_value)
else getattr(self._device, self._min_value)
)
def native_min_value(self) -> float:
return cast(
float,
(
self._min_value
if isinstance(self._min_value, int)
else (
self._device.get_attribute(attr=self._min_value)
if self._device.get_attribute(attr=self._min_value)
else getattr(self._device, self._min_value)
)
),
)

@property
def native_max_value(self):
return (
self._max_value
if isinstance(self._max_value, int)
else (
self._device.get_attribute(attr=self._max_value)
if self._device.get_attribute(attr=self._max_value)
else getattr(self._device, self._max_value)
)
def native_max_value(self) -> float:
return cast(
float,
(
self._max_value
if isinstance(self._max_value, int)
else (
self._device.get_attribute(attr=self._max_value)
if self._device.get_attribute(attr=self._max_value)
else getattr(self._device, self._max_value)
)
),
)

@property
def native_step(self):
return (
self._step_value
if isinstance(self._step_value, int)
else (
self._device.get_attribute(attr=self._step_value)
if self._device.get_attribute(attr=self._step_value)
else getattr(self._device, self._step_value)
)
def native_step(self) -> float:
return cast(
float,
(
self._step_value
if isinstance(self._step_value, int)
else (
self._device.get_attribute(attr=self._step_value)
if self._device.get_attribute(attr=self._step_value)
else getattr(self._device, self._step_value)
)
),
)

@property
def native_value(self):
return self._device.get_attribute(self._entity_key)
def native_value(self) -> float:
return cast(float, self._device.get_attribute(self._entity_key))

def set_native_value(self, value):
def set_native_value(self, value: Any) -> None:
self._device.set_attribute(self._entity_key, value)
26 changes: 18 additions & 8 deletions custom_components/midea_ac_lan/select.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
from typing import Any, cast
from homeassistant.components.select import SelectEntity
from homeassistant.const import CONF_DEVICE_ID, CONF_SWITCHES, Platform

from .const import DEVICES, DOMAIN
from .midea_devices import MIDEA_DEVICES
from .midea_entity import MideaEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback


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, [])
selects = []
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.SELECT and entity_key in extra_switches:
dev = MideaSelect(device, entity_key)
selects.append(dev)
async_add_entities(selects)


class MideaSelect(MideaEntity, SelectEntity):
def __init__(self, device, entity_key: str):
def __init__(self, device: Any, entity_key: str) -> None:
super().__init__(device, entity_key)
self._options_name = self._config.get("options")

@property
def options(self):
return getattr(self._device, self._options_name)
def options(self) -> list[str]:
return cast(list, getattr(self._device, self._options_name))

@property
def current_option(self):
return self._device.get_attribute(self._entity_key)
def current_option(self) -> str:
return cast(str, self._device.get_attribute(self._entity_key))

def select_option(self, option: str):
def select_option(self, option: str) -> None:
self._device.set_attribute(self._entity_key, option)
38 changes: 25 additions & 13 deletions custom_components/midea_ac_lan/sensor.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
from typing import Any, cast
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import CONF_DEVICE_ID, CONF_SENSORS, Platform

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):
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.helpers.typing import StateType


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_sensors = config_entry.options.get(CONF_SENSORS, [])
sensors = []
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.SENSOR and entity_key in extra_sensors:
sensor = MideaSensor(device, entity_key)
sensors.append(sensor)
Expand All @@ -20,21 +32,21 @@ async def async_setup_entry(hass, config_entry, async_add_entities):

class MideaSensor(MideaEntity, SensorEntity):
@property
def native_value(self):
return self._device.get_attribute(self._entity_key)
def native_value(self) -> StateType:
return cast(StateType, self._device.get_attribute(self._entity_key))

@property
def device_class(self):
return self._config.get("device_class")
def device_class(self) -> SensorDeviceClass:
return cast(SensorDeviceClass, self._config.get("device_class"))

@property
def state_class(self):
return self._config.get("state_class")
def state_class(self) -> SensorStateClass | None:
return cast(SensorStateClass | None, self._config.get("state_class"))

@property
def native_unit_of_measurement(self):
return self._config.get("unit")
def native_unit_of_measurement(self) -> str | None:
return cast(str | None, self._config.get("unit"))

@property
def capability_attributes(self):
def capability_attributes(self) -> dict[str, Any] | None:
return {"state_class": self.state_class} if self.state_class else {}
20 changes: 15 additions & 5 deletions custom_components/midea_ac_lan/switch.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from typing import Any, cast
from homeassistant.const import CONF_DEVICE_ID, CONF_SWITCHES, Platform
from homeassistant.helpers.entity import ToggleEntity

from .const import DEVICES, DOMAIN
from .midea_devices import MIDEA_DEVICES
from .midea_entity import MideaEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback


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, [])
switches = []
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.SWITCH and entity_key in extra_switches:
dev = MideaSwitch(device, entity_key)
switches.append(dev)
Expand All @@ -21,10 +31,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class MideaSwitch(MideaEntity, ToggleEntity):
@property
def is_on(self) -> bool:
return self._device.get_attribute(self._entity_key)
return cast(bool, self._device.get_attribute(self._entity_key))

def turn_on(self):
def turn_on(self, **kwargs: Any) -> None:
self._device.set_attribute(attr=self._entity_key, value=True)

def turn_off(self):
def turn_off(self, **kwargs: Any) -> None:
self._device.set_attribute(attr=self._entity_key, value=False)

0 comments on commit 1265537

Please sign in to comment.