Files

123 lines
4.0 KiB
Python
Raw Permalink Normal View History

2023-09-09 00:14:41 +08:00
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorDeviceClass
)
2025-09-12 00:15:14 +08:00
from homeassistant.const import Platform
2025-09-09 23:52:48 +08:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2023-09-10 12:08:27 +08:00
2025-09-12 00:15:14 +08:00
from .const import DOMAIN
2025-09-09 23:52:48 +08:00
from .midea_entity import MideaEntity
2025-09-12 00:15:14 +08:00
from . import load_device_config
2023-09-02 16:30:03 +08:00
2025-09-09 23:52:48 +08:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up binary sensor entities for Midea devices."""
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", {})
devs = []
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.BINARY_SENSOR, {})
manufacturer = config.get("manufacturer")
rationale = config.get("rationale")
coordinator = coordinator_map.get(device_id)
device = coordinator.device if coordinator else None
# 连接状态实体
if coordinator and device:
devs.append(MideaDeviceStatusSensorEntity(coordinator, device, manufacturer, rationale, "Status", {}))
for entity_key, ecfg in entities_cfg.items():
2025-09-09 23:52:48 +08:00
devs.append(MideaBinarySensorEntity(
2025-09-12 00:15:14 +08:00
coordinator, device, manufacturer, rationale, entity_key, ecfg
2025-09-09 23:52:48 +08:00
))
2023-09-09 00:14:41 +08:00
async_add_entities(devs)
2023-09-02 16:30:03 +08:00
2025-09-09 23:52:48 +08:00
class MideaDeviceStatusSensorEntity(MideaEntity, BinarySensorEntity):
"""Device status binary sensor."""
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,
2025-09-09 23:52:48 +08:00
)
self._device = device
self._manufacturer = manufacturer
self._rationale = rationale
self._config = config
2023-09-02 16:30:03 +08:00
@property
def device_class(self):
2025-09-09 23:52:48 +08:00
"""Return the device class."""
2023-09-02 16:30:03 +08:00
return BinarySensorDeviceClass.CONNECTIVITY
2023-09-03 22:15:41 +08:00
@property
def icon(self):
2025-09-09 23:52:48 +08:00
"""Return the icon."""
2023-09-03 22:15:41 +08:00
return "mdi:devices"
2023-09-02 16:30:03 +08:00
@property
def is_on(self):
2025-09-09 23:52:48 +08:00
"""Return if the device is connected."""
return self.coordinator.data.connected
2023-09-02 16:30:03 +08:00
@property
def extra_state_attributes(self) -> dict:
2025-09-09 23:52:48 +08:00
"""Return extra state attributes."""
return self.device_attributes
2023-09-02 16:30:03 +08:00
2023-09-09 00:14:41 +08:00
2025-09-09 23:52:48 +08:00
class MideaBinarySensorEntity(MideaEntity, BinarySensorEntity):
"""Generic binary sensor entity."""
2023-09-09 00:14:41 +08:00
2025-09-09 23:52:48 +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,
2025-09-09 23:52:48 +08:00
)
self._device = device
self._manufacturer = manufacturer
self._rationale = rationale
self._entity_key = entity_key
self._config = config
@property
def is_on(self):
"""Return if the binary sensor 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"