Skip to content

Commit

Permalink
Add self/active clean support (#180)
Browse files Browse the repository at this point in the history
* Add binary sensor for self clean

* Add button entity to start self clean

* Add icon for self clean

* Update README
  • Loading branch information
mill1000 authored Jul 18, 2024
1 parent d8fb192 commit 5583b6e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 supported<sup>3</sup>.
* Button and binary sensor to start & monitor self cleaning.
* Translations
* български
* Català
Expand Down
1 change: 1 addition & 0 deletions custom_components/midea_ac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
_LOGGER = logging.getLogger(__name__)
_PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.CLIMATE,
Platform.NUMBER,
Platform.SELECT,
Expand Down
22 changes: 16 additions & 6 deletions custom_components/midea_ac/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
85 changes: 85 additions & 0 deletions custom_components/midea_ac/button.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions custom_components/midea_ac/icons.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"entity": {
"binary_sensor": {
"self_clean": {
"default": "mdi:spray-bottle"
}
},
"number": {
"fan_speed": {
"default": "mdi:fan"
Expand Down
8 changes: 8 additions & 0 deletions custom_components/midea_ac/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
"binary_sensor": {
"filter_alert": {
"name": "Filter alert"
},
"self_clean": {
"name": "Self clean"
}
},
"button": {
"self_clean": {
"name": "Start self clean"
}
},
"number": {
Expand Down

0 comments on commit 5583b6e

Please sign in to comment.