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

96 lines
3.3 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-02 16:30:03 +08:00
class MideaEntity(Entity):
2023-09-09 00:14:41 +08:00
def __init__(self, device, manufacturer: str | 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-09 00:14:41 +08:00
self._attr_native_unit_of_measurement = self._config.get("unit")
self._attr_device_class = self._config.get("device_class")
self._attr_state_class = self._config.get("state_class")
self._attr_unit_of_measurement = self._config.get("unit")
self._attr_icon = self._config.get("icon")
self._attr_unique_id = f"{DOMAIN}.{self._device.device_id}_{self._entity_key}"
MideaLogger.debug(self._attr_unique_id)
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
@property
def should_poll(self):
return False
@property
def state(self):
2023-09-09 00:14:41 +08:00
raise NotImplementedError
2023-09-02 16:30:03 +08:00
@property
def available(self):
return self._device.connected
2023-09-09 00:14:41 +08:00
def get_mode(self, key_of_modes, rationale: Rationale = Rationale.EQUALLY):
for mode, status in key_of_modes.items():
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:
2023-09-09 00:14:41 +08:00
2023-09-02 16:30:03 +08:00
try:
self.schedule_update_ha_state()
except Exception as e:
2023-09-09 00:14:41 +08:00
pass
class MideaBinaryBaseEntity(MideaEntity):
def __init__(self, device, manufacturer: str | None, entity_key: str, config: dict):
super().__init__(device, manufacturer, entity_key, config)
binary_rationale = config.get("binary_rationale")
self._binary_rationale = binary_rationale if binary_rationale is not None else ["off", "on"]
@property
def state(self):
return STATE_ON if self.is_on else STATE_OFF
@property
def is_on(self):
return self._device.get_attribute(self._entity_key) == self._binary_rationale[1]