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

60 lines
1.4 KiB
Python
Raw Normal View History

2023-09-02 16:30:03 +08:00
import logging
from homeassistant.const import (
CONF_DEVICE_ID,
STATE_ON,
STATE_OFF
)
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from .midea_entities import MideaEntity
from .const import (
DOMAIN,
DEVICES,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
device_id = config_entry.data.get(CONF_DEVICE_ID)
device = hass.data[DOMAIN][DEVICES].get(device_id)
binary_sensors = []
2023-09-03 22:15:41 +08:00
sensor = MideaDeviceStatusSensor(device, "status")
2023-09-02 16:30:03 +08:00
binary_sensors.append(sensor)
async_add_entities(binary_sensors)
class MideaDeviceStatusSensor(MideaEntity):
@property
def device_class(self):
return BinarySensorDeviceClass.CONNECTIVITY
@property
def state(self):
return STATE_ON if self._device.connected else STATE_OFF
2023-09-03 22:15:41 +08:00
@property
def name(self):
return f"{self._device_name} Status"
@property
def icon(self):
return "mdi:devices"
2023-09-02 16:30:03 +08:00
@property
def is_on(self):
return self.state == STATE_ON
@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