Files
midea-meiju-codec/custom_components/midea_meiju_codec/midea_entities.py

130 lines
4.6 KiB
Python
Raw Normal View History

2023-09-09 00:14:41 +08:00
from enum import IntEnum
2023-09-02 16:30:03 +08:00
from homeassistant.helpers.entity import Entity
2023-09-09 00:14:41 +08:00
from homeassistant.const import (
STATE_ON,
STATE_OFF
)
2023-09-02 16:30:03 +08:00
from .const import DOMAIN
2023-09-09 00:14:41 +08:00
from .core.logger import MideaLogger
2023-09-02 16:30:03 +08:00
2023-09-09 00:14:41 +08:00
class Rationale(IntEnum):
EQUALLY = 0
GREATER = 1
LESS = 2
2023-09-10 12:08:27 +08:00
2023-09-02 16:30:03 +08:00
class MideaEntity(Entity):
2023-09-10 12:08:27 +08:00
def __init__(self, device, manufacturer: str | None, rationale: list | None, entity_key: str, config: dict):
2023-09-02 16:30:03 +08:00
self._device = device
self._device.register_update(self.update_state)
self._entity_key = entity_key
2023-09-09 00:14:41 +08:00
self._config = config
2023-09-03 22:15:41 +08:00
self._device_name = self._device.device_name
2023-09-10 12:08:27 +08:00
self._rationale = rationale
2023-09-17 19:40:54 +08:00
if rationale_local := config.get("rationale"):
self._rationale = rationale_local
2023-09-10 12:08:27 +08:00
if self._rationale is None:
self._rationale = ["off", "on"]
self._attr_native_unit_of_measurement = self._config.get("unit_of_measurement")
2023-09-09 00:14:41 +08:00
self._attr_device_class = self._config.get("device_class")
self._attr_state_class = self._config.get("state_class")
self._attr_icon = self._config.get("icon")
self._attr_unique_id = f"{DOMAIN}.{self._device.device_id}_{self._entity_key}"
self._attr_device_info = {
"manufacturer": "Midea" if manufacturer is None else manufacturer,
"model": f"{self._device.model}",
2023-09-02 16:30:03 +08:00
"identifiers": {(DOMAIN, self._device.device_id)},
"name": self._device_name
}
2023-09-09 00:14:41 +08:00
name = self._config.get("name")
if name is None:
name = self._entity_key.replace("_", " ").title()
self._attr_name = f"{self._device_name} {name}"
self.entity_id = self._attr_unique_id
2023-09-02 16:30:03 +08:00
2023-09-17 19:40:54 +08:00
@property
def device(self):
return self._device
2023-09-02 16:30:03 +08:00
@property
def should_poll(self):
return False
@property
def available(self):
return self._device.connected
2023-09-10 12:08:27 +08:00
def _get_status_on_off(self, status_key: str):
result = False
status = self._device.get_attribute(status_key)
if status is not None:
try:
result = bool(self._rationale.index(status))
except ValueError:
MideaLogger.error(f"The value of attribute {status_key} ('{status}') "
f"is not in rationale {self._rationale}")
return result
def _set_status_on_off(self, status_key: str, turn_on: bool):
self._device.set_attribute(status_key, self._rationale[int(turn_on)])
def _list_get_selected(self, key_of_list: list, rationale: Rationale = Rationale.EQUALLY):
for index in range(0, len(key_of_list)):
match = True
for attr, value in key_of_list[index].items():
state_value = self._device.get_attribute(attr)
if state_value is None:
match = False
break
if rationale is Rationale.EQUALLY and state_value != value:
match = False
break
if rationale is Rationale.GREATER and state_value < value:
match = False
break
if rationale is Rationale.LESS and state_value > value:
match = False
break
if match:
return index
return None
def _dict_get_selected(self, key_of_dict: dict, rationale: Rationale = Rationale.EQUALLY):
for mode, status in key_of_dict.items():
2023-09-09 00:14:41 +08:00
match = True
for attr, value in status.items():
state_value = self._device.get_attribute(attr)
if state_value is None:
match = False
break
if rationale is Rationale.EQUALLY and state_value != value:
match = False
break
if rationale is Rationale.GREATER and state_value < value:
match = False
break
if rationale is Rationale.LESS and state_value > value:
match = False
break
if match:
return mode
return None
2023-09-02 16:30:03 +08:00
def update_state(self, status):
if self._entity_key in status or "connected" in status:
try:
self.schedule_update_ha_state()
except Exception as e:
2023-09-09 00:14:41 +08:00
pass
class MideaBinaryBaseEntity(MideaEntity):
@property
def state(self):
return STATE_ON if self.is_on else STATE_OFF
@property
def is_on(self):
2023-09-10 12:08:27 +08:00
return self._get_status_on_off(self._entity_key)