-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5411738
commit 1265537
Showing
4 changed files
with
110 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters