Files
midea-meiju-codec/custom_components/midea_auto_codec/binary_sensor.py

120 lines
3.6 KiB
Python
Raw Normal View History

2023-09-09 00:14:41 +08:00
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorDeviceClass
)
2023-09-02 16:30:03 +08:00
from homeassistant.const import (
2023-09-09 00:14:41 +08:00
Platform,
2023-09-02 16:30:03 +08:00
CONF_DEVICE_ID,
2025-09-09 23:52:48 +08:00
CONF_ENTITIES, CONF_DEVICE
2023-09-02 16:30:03 +08:00
)
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
2023-09-02 16:30:03 +08:00
from .const import (
DOMAIN,
2023-09-09 00:14:41 +08:00
DEVICES
2023-09-02 16:30:03 +08:00
)
2025-09-09 23:52:48 +08:00
from .midea_entity import MideaEntity
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."""
2023-09-02 16:30:03 +08:00
device_id = config_entry.data.get(CONF_DEVICE_ID)
2025-09-09 23:52:48 +08:00
device_data = hass.data[DOMAIN][DEVICES][device_id]
coordinator = device_data.get("coordinator")
device = device_data.get(CONF_DEVICE)
manufacturer = device_data.get("manufacturer")
rationale = device_data.get("rationale")
entities = device_data.get(CONF_ENTITIES, {}).get(Platform.BINARY_SENSOR, {})
devs = [MideaDeviceStatusSensorEntity(coordinator, device, manufacturer, rationale, "Status", {})]
if entities:
2023-09-09 00:14:41 +08:00
for entity_key, config in entities.items():
2025-09-09 23:52:48 +08:00
devs.append(MideaBinarySensorEntity(
coordinator, device, manufacturer, rationale, entity_key, config
))
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,
)
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 "status"
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,
)
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"binary_sensor_{self._entity_key}"
@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"