mirror of
https://github.com/sususweet/midea-meiju-codec.git
synced 2025-09-27 18:22:41 +00:00
Rename project
This commit is contained in:
81
custom_components/midea_auto_cloud/switch.py
Normal file
81
custom_components/midea_auto_cloud/switch.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .core.logger import MideaLogger
|
||||
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:
|
||||
"""Set up switch entities for Midea devices."""
|
||||
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 = load_device_config(hass, device_type, sn8) or {}
|
||||
entities_cfg = (config.get("entities") or {}).get(Platform.SWITCH, {})
|
||||
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(MideaSwitchEntity(
|
||||
coordinator, device, manufacturer, rationale, entity_key, ecfg
|
||||
))
|
||||
async_add_entities(devs)
|
||||
|
||||
|
||||
class MideaSwitchEntity(MideaEntity, SwitchEntity):
|
||||
"""Midea switch entity."""
|
||||
|
||||
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,
|
||||
)
|
||||
self._device = device
|
||||
self._manufacturer = manufacturer
|
||||
self._rationale = rationale
|
||||
self._entity_key = entity_key
|
||||
self._config = config
|
||||
|
||||
@property
|
||||
def entity_id_suffix(self) -> str:
|
||||
"""Return the suffix for entity ID."""
|
||||
return f"switch_{self._entity_key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return if the switch is on."""
|
||||
value = self.device_attributes.get(self._entity_key)
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
return value == 1 or value == "on" or value == "true"
|
||||
|
||||
async def async_turn_on(self):
|
||||
"""Turn the switch on."""
|
||||
await self.async_set_attribute(self._entity_key, True)
|
||||
|
||||
async def async_turn_off(self):
|
||||
"""Turn the switch off."""
|
||||
await self.async_set_attribute(self._entity_key, False)
|
Reference in New Issue
Block a user