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

63 lines
1.7 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,
2023-09-09 00:14:41 +08:00
CONF_DEVICE,
CONF_ENTITIES
2023-09-02 16:30:03 +08:00
)
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
)
2023-09-09 00:14:41 +08:00
from .midea_entities import MideaBinaryBaseEntity
2023-09-02 16:30:03 +08:00
async def async_setup_entry(hass, config_entry, async_add_entities):
device_id = config_entry.data.get(CONF_DEVICE_ID)
2023-09-09 00:14:41 +08:00
device = hass.data[DOMAIN][DEVICES][device_id].get(CONF_DEVICE)
manufacturer = hass.data[DOMAIN][DEVICES][device_id].get("manufacturer")
2023-09-10 12:08:27 +08:00
rationale = hass.data[DOMAIN][DEVICES][device_id].get("rationale")
2023-09-09 00:14:41 +08:00
entities = hass.data[DOMAIN][DEVICES][device_id].get(CONF_ENTITIES).get(Platform.BINARY_SENSOR)
2023-09-10 12:08:27 +08:00
devs = [MideaDeviceStatusSensorEntity(device, manufacturer, rationale,"Status", {})]
2023-09-09 00:14:41 +08:00
if entities is not None:
for entity_key, config in entities.items():
2023-09-10 12:08:27 +08:00
devs.append(MideaBinarySensorEntity(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
2023-09-09 00:14:41 +08:00
class MideaDeviceStatusSensorEntity(MideaBinaryBaseEntity, BinarySensorEntity):
2023-09-02 16:30:03 +08:00
@property
def device_class(self):
return BinarySensorDeviceClass.CONNECTIVITY
2023-09-03 22:15:41 +08:00
@property
def icon(self):
return "mdi:devices"
2023-09-02 16:30:03 +08:00
@property
def is_on(self):
2023-09-09 00:14:41 +08:00
return self._device.connected
2023-09-02 16:30:03 +08:00
@property
def available(self):
return True
@property
def extra_state_attributes(self) -> dict:
return self._device.attributes
def update_state(self, status):
try:
self.schedule_update_ha_state()
except Exception as e:
pass
2023-09-09 00:14:41 +08:00
class MideaBinarySensorEntity(MideaBinaryBaseEntity, BinarySensorEntity):
pass