2023-09-10 12:08:27 +08:00
|
|
|
from homeassistant.components.select import SelectEntity
|
2025-09-12 00:15:14 +08:00
|
|
|
from homeassistant.const import Platform
|
|
|
|
from .const import DOMAIN
|
2025-09-24 19:57:11 +08:00
|
|
|
from .midea_entity import MideaEntity
|
2025-09-12 00:15:14 +08:00
|
|
|
from . import load_device_config
|
2023-09-10 12:08:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2025-09-12 00:15:14 +08:00
|
|
|
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", {})
|
|
|
|
|
2023-09-10 12:08:27 +08:00
|
|
|
devs = []
|
2025-09-12 00:15:14 +08:00
|
|
|
for device_id, info in device_list.items():
|
|
|
|
device_type = info.get("type")
|
|
|
|
sn8 = info.get("sn8")
|
2025-09-17 23:15:15 +08:00
|
|
|
config = await load_device_config(hass, device_type, sn8) or {}
|
2025-09-12 00:15:14 +08:00
|
|
|
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():
|
2025-09-24 19:57:11 +08:00
|
|
|
devs.append(MideaSelectEntity(coordinator, device, manufacturer, rationale, entity_key, ecfg))
|
2023-09-10 12:08:27 +08:00
|
|
|
async_add_entities(devs)
|
|
|
|
|
|
|
|
|
|
|
|
class MideaSelectEntity(MideaEntity, SelectEntity):
|
2025-09-24 19:57:11 +08:00
|
|
|
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._device = device
|
|
|
|
self._manufacturer = manufacturer
|
|
|
|
self._rationale = rationale
|
|
|
|
self._config = config
|
2023-09-10 12:08:27 +08:00
|
|
|
self._key_options = self._config.get("options")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def options(self):
|
|
|
|
return list(self._key_options.keys())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_option(self):
|
|
|
|
return self._dict_get_selected(self._key_options)
|
|
|
|
|
2025-09-24 19:57:11 +08:00
|
|
|
async def async_select_option(self, option: str):
|
2023-09-10 12:08:27 +08:00
|
|
|
new_status = self._key_options.get(option)
|
2025-09-24 19:57:11 +08:00
|
|
|
if new_status:
|
|
|
|
await self.async_set_attributes(new_status)
|
2023-09-10 12:08:27 +08:00
|
|
|
|
|
|
|
def update_state(self, status):
|
|
|
|
try:
|
|
|
|
self.schedule_update_ha_state()
|
2025-09-24 19:57:11 +08:00
|
|
|
except Exception:
|
2023-09-10 12:08:27 +08:00
|
|
|
pass
|
|
|
|
|