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

69 lines
2.1 KiB
Python
Raw Normal View History

2023-09-09 00:14:41 +08:00
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import (
Platform,
CONF_DEVICE_ID,
2025-09-09 23:52:48 +08:00
CONF_ENTITIES, CONF_DEVICE
2023-09-09 00:14:41 +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-09 00:14:41 +08:00
from .const import (
DOMAIN,
DEVICES
)
2025-09-09 23:52:48 +08:00
from .midea_entity import MideaEntity
2023-09-09 00:14:41 +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 sensor entities for Midea devices."""
2023-09-09 00:14:41 +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.SENSOR, {})
2023-09-09 00:14:41 +08:00
devs = []
2025-09-09 23:52:48 +08:00
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(MideaSensorEntity(
coordinator, device, manufacturer, rationale, entity_key, config
))
2023-09-09 00:14:41 +08:00
async_add_entities(devs)
class MideaSensorEntity(MideaEntity, SensorEntity):
2025-09-09 23:52:48 +08:00
"""Midea sensor 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"sensor_{self._entity_key}"
2023-09-09 00:14:41 +08:00
@property
2023-09-10 12:08:27 +08:00
def native_value(self):
2025-09-09 23:52:48 +08:00
"""Return the native value of the sensor."""
return self.device_attributes.get(self._entity_key)