mirror of
https://github.com/sususweet/midea-meiju-codec.git
synced 2025-10-15 18:58:29 +00:00
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
from homeassistant.components.select import SelectEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .const import DOMAIN
|
|
from .midea_entity import MideaEntity
|
|
from . import load_device_config
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
account_bucket = hass.data.get(DOMAIN, {}).get("accounts", {}).get(config_entry.entry_id)
|
|
if not account_bucket:
|
|
async_add_entities([])
|
|
return
|
|
device_list = account_bucket.get("device_list", {})
|
|
coordinator_map = account_bucket.get("coordinator_map", {})
|
|
|
|
devs = []
|
|
for device_id, info in device_list.items():
|
|
device_type = info.get("type")
|
|
sn8 = info.get("sn8")
|
|
config = await load_device_config(hass, device_type, sn8) or {}
|
|
entities_cfg = (config.get("entities") or {}).get(Platform.SELECT, {})
|
|
manufacturer = config.get("manufacturer")
|
|
rationale = config.get("rationale")
|
|
coordinator = coordinator_map.get(device_id)
|
|
device = coordinator.device if coordinator else None
|
|
for entity_key, ecfg in entities_cfg.items():
|
|
devs.append(MideaSelectEntity(coordinator, device, manufacturer, rationale, entity_key, ecfg))
|
|
async_add_entities(devs)
|
|
|
|
|
|
class MideaSelectEntity(MideaEntity, SelectEntity):
|
|
def __init__(self, coordinator, device, manufacturer, rationale, entity_key, config):
|
|
super().__init__(
|
|
coordinator,
|
|
device.device_id,
|
|
device.device_name,
|
|
f"T0x{device.device_type:02X}",
|
|
device.sn,
|
|
device.sn8,
|
|
device.model,
|
|
entity_key,
|
|
device=device,
|
|
manufacturer=manufacturer,
|
|
rationale=rationale,
|
|
config=config,
|
|
)
|
|
self._key_options = self._config.get("options")
|
|
|
|
@property
|
|
def options(self):
|
|
return list(self._key_options.keys())
|
|
|
|
@property
|
|
def current_option(self):
|
|
# Use attribute from config if available, otherwise fall back to entity_key
|
|
attribute = self._config.get("attribute", self._entity_key)
|
|
if attribute and attribute != self._entity_key:
|
|
# For simple attribute access, get the value directly
|
|
return self._get_nested_value(attribute)
|
|
else:
|
|
# For complex mapping, use the existing logic
|
|
return self._dict_get_selected(self._key_options)
|
|
|
|
async def async_select_option(self, option: str):
|
|
new_status = self._key_options.get(option)
|
|
if new_status:
|
|
await self.async_set_attributes(new_status)
|
|
|
|
def update_state(self, status):
|
|
try:
|
|
self.schedule_update_ha_state()
|
|
except Exception:
|
|
pass
|
|
|