diff --git a/README.md b/README.md index 378b764..c9c1a1e 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ See [Getting Device Info](#getting-device-info) to determine if a device is supp * Select entities to control swing angle when supported. * Indoor humidity sensor when supported. * Energy and power sensors when supported3. +* Button and binary sensor to start & monitor self cleaning. * Translations * български * Català diff --git a/custom_components/midea_ac/__init__.py b/custom_components/midea_ac/__init__.py index 06aa57d..ead4a22 100644 --- a/custom_components/midea_ac/__init__.py +++ b/custom_components/midea_ac/__init__.py @@ -18,6 +18,7 @@ _LOGGER = logging.getLogger(__name__) _PLATFORMS = [ Platform.BINARY_SENSOR, + Platform.BUTTON, Platform.CLIMATE, Platform.NUMBER, Platform.SELECT, diff --git a/custom_components/midea_ac/binary_sensor.py b/custom_components/midea_ac/binary_sensor.py index 8f0ea4d..472eb49 100644 --- a/custom_components/midea_ac/binary_sensor.py +++ b/custom_components/midea_ac/binary_sensor.py @@ -29,13 +29,23 @@ async def async_setup_entry( # Fetch coordinator from global data coordinator = hass.data[DOMAIN][config_entry.entry_id] - # Create sensor entities from device if supported + # Create entities for supported features + entities = [] if getattr(coordinator.device, "supports_filter_reminder", False): - add_entities([MideaBinarySensor(coordinator, - "filter_alert", - BinarySensorDeviceClass.PROBLEM, - "filter_alert" - )]) + entities.append(MideaBinarySensor(coordinator, + "filter_alert", + BinarySensorDeviceClass.PROBLEM, + "filter_alert" + )) + + if getattr(coordinator.device, "supports_self_clean", False): + entities.append(MideaBinarySensor(coordinator, + "self_clean_active", + BinarySensorDeviceClass.RUNNING, + "self_clean", + entity_category=EntityCategory.DIAGNOSTIC, + )) + add_entities(entities) class MideaBinarySensor(MideaCoordinatorEntity, BinarySensorEntity): diff --git a/custom_components/midea_ac/button.py b/custom_components/midea_ac/button.py new file mode 100644 index 0000000..ebe1e7c --- /dev/null +++ b/custom_components/midea_ac/button.py @@ -0,0 +1,85 @@ +"""Button platform for Midea Smart AC.""" +from __future__ import annotations + +import logging +from typing import Optional + +from homeassistant.components.button import ButtonEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity import EntityCategory +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .coordinator import MideaCoordinatorEntity, MideaDeviceUpdateCoordinator + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + add_entities: AddEntitiesCallback, +) -> None: + """Setup the button platform for Midea Smart AC.""" + + _LOGGER.info("Setting up button platform.") + + # Fetch coordinator from global data + coordinator = hass.data[DOMAIN][config_entry.entry_id] + + # Create entities for supported features + entities = [] + if getattr(coordinator.device, "supports_self_clean", False): + entities.append(MideaButton(coordinator, + "start_self_clean", + "self_clean", + entity_category=EntityCategory.DIAGNOSTIC, + )) + add_entities(entities) + + +class MideaButton(MideaCoordinatorEntity, ButtonEntity): + """Button for Midea AC.""" + + def __init__(self, + coordinator: MideaDeviceUpdateCoordinator, + method: str, + translation_key: Optional[str] = None, + *, + entity_category: EntityCategory = None) -> None: + MideaCoordinatorEntity.__init__(self, coordinator) + + self._method = method + self._entity_category = entity_category + self._attr_translation_key = translation_key + + @property + def device_info(self) -> dict: + """Return info for device registry.""" + return { + "identifiers": { + (DOMAIN, self._device.id) + }, + } + + @property + def has_entity_name(self) -> bool: + """Indicates if entity follows naming conventions.""" + return True + + @property + def unique_id(self) -> str: + """Return the unique ID of this entity.""" + return f"{self._device.id}-{self._method}" + + @property + def entity_category(self) -> str: + """Return the entity category of this entity.""" + return self._entity_category + + async def async_press(self) -> None: + """Handle the button press.""" + # Call the buttons method + if method := getattr(self._device, self._method, None): + await method() diff --git a/custom_components/midea_ac/icons.json b/custom_components/midea_ac/icons.json index f21e080..1f11429 100644 --- a/custom_components/midea_ac/icons.json +++ b/custom_components/midea_ac/icons.json @@ -1,5 +1,10 @@ { "entity": { + "binary_sensor": { + "self_clean": { + "default": "mdi:spray-bottle" + } + }, "number": { "fan_speed": { "default": "mdi:fan" diff --git a/custom_components/midea_ac/translations/en.json b/custom_components/midea_ac/translations/en.json index 11d331f..24be724 100644 --- a/custom_components/midea_ac/translations/en.json +++ b/custom_components/midea_ac/translations/en.json @@ -100,6 +100,14 @@ "binary_sensor": { "filter_alert": { "name": "Filter alert" + }, + "self_clean": { + "name": "Self clean" + } + }, + "button": { + "self_clean": { + "name": "Start self clean" } }, "number": {