From f5c0b790f004ffde3fb09aa512f23d4081407c50 Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jul 2024 12:56:59 -0600 Subject: [PATCH 1/7] Add binary sensor for self clean --- custom_components/midea_ac/binary_sensor.py | 12 +++++++++++- custom_components/midea_ac/translations/en.json | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/custom_components/midea_ac/binary_sensor.py b/custom_components/midea_ac/binary_sensor.py index 8f0ea4d..a43a90c 100644 --- a/custom_components/midea_ac/binary_sensor.py +++ b/custom_components/midea_ac/binary_sensor.py @@ -29,7 +29,8 @@ 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", @@ -37,6 +38,15 @@ async def async_setup_entry( "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): """Binary sensor for Midea AC.""" diff --git a/custom_components/midea_ac/translations/en.json b/custom_components/midea_ac/translations/en.json index 11d331f..1271002 100644 --- a/custom_components/midea_ac/translations/en.json +++ b/custom_components/midea_ac/translations/en.json @@ -100,6 +100,9 @@ "binary_sensor": { "filter_alert": { "name": "Filter alert" + }, + "self_clean": { + "name": "Self clean" } }, "number": { From 6511ceb9b326c328d7ac2e8e367accf44b16d195 Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jul 2024 13:19:47 -0600 Subject: [PATCH 2/7] Add button entity to start self clean --- custom_components/midea_ac/__init__.py | 1 + custom_components/midea_ac/button.py | 84 +++++++++++++++++++ .../midea_ac/translations/en.json | 5 ++ 3 files changed, 90 insertions(+) create mode 100644 custom_components/midea_ac/button.py 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/button.py b/custom_components/midea_ac/button.py new file mode 100644 index 0000000..05b97a8 --- /dev/null +++ b/custom_components/midea_ac/button.py @@ -0,0 +1,84 @@ +"""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", + EntityCategory.DIAGNOSTIC, + "self_clean" + )) + add_entities(entities) + + +class MideaButton(MideaCoordinatorEntity, ButtonEntity): + """Button for Midea AC.""" + + def __init__(self, + coordinator: MideaDeviceUpdateCoordinator, + method: str, + entity_category: EntityCategory, + translation_key: Optional[str] = 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._prop}" + + @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() \ No newline at end of file diff --git a/custom_components/midea_ac/translations/en.json b/custom_components/midea_ac/translations/en.json index 1271002..24be724 100644 --- a/custom_components/midea_ac/translations/en.json +++ b/custom_components/midea_ac/translations/en.json @@ -105,6 +105,11 @@ "name": "Self clean" } }, + "button": { + "self_clean": { + "name": "Start self clean" + } + }, "number": { "fan_speed": { "name": "Fan speed" From 3ed0974dc9b2b273dd6565f6dc0d69ab10a25031 Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jul 2024 14:01:16 -0600 Subject: [PATCH 3/7] Fix bad reference to _prop in button entity --- custom_components/midea_ac/button.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/midea_ac/button.py b/custom_components/midea_ac/button.py index 05b97a8..fd312b1 100644 --- a/custom_components/midea_ac/button.py +++ b/custom_components/midea_ac/button.py @@ -70,7 +70,7 @@ def has_entity_name(self) -> bool: @property def unique_id(self) -> str: """Return the unique ID of this entity.""" - return f"{self._device.id}-{self._prop}" + return f"{self._device.id}-{self._method}" @property def entity_category(self) -> str: From 4ad1c50b48f88e22289cb215e866b69ca01ea93b Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jul 2024 14:01:41 -0600 Subject: [PATCH 4/7] Add icon for self clean --- custom_components/midea_ac/icons.json | 5 +++++ 1 file changed, 5 insertions(+) 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" From 6ad5efe55158b8c2aae9fba03c6b54da7446beba Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jul 2024 14:07:03 -0600 Subject: [PATCH 5/7] Make entity_category a kwarg --- custom_components/midea_ac/button.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/custom_components/midea_ac/button.py b/custom_components/midea_ac/button.py index fd312b1..ebe1e7c 100644 --- a/custom_components/midea_ac/button.py +++ b/custom_components/midea_ac/button.py @@ -32,10 +32,10 @@ async def async_setup_entry( entities = [] if getattr(coordinator.device, "supports_self_clean", False): entities.append(MideaButton(coordinator, - "start_self_clean", - EntityCategory.DIAGNOSTIC, - "self_clean" - )) + "start_self_clean", + "self_clean", + entity_category=EntityCategory.DIAGNOSTIC, + )) add_entities(entities) @@ -45,8 +45,9 @@ class MideaButton(MideaCoordinatorEntity, ButtonEntity): def __init__(self, coordinator: MideaDeviceUpdateCoordinator, method: str, - entity_category: EntityCategory, - translation_key: Optional[str] = None) -> None: + translation_key: Optional[str] = None, + *, + entity_category: EntityCategory = None) -> None: MideaCoordinatorEntity.__init__(self, coordinator) self._method = method @@ -81,4 +82,4 @@ async def async_press(self) -> None: """Handle the button press.""" # Call the buttons method if method := getattr(self._device, self._method, None): - await method() \ No newline at end of file + await method() From aa5e3704276f078009206033435d26f77aa58f64 Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jul 2024 14:08:53 -0600 Subject: [PATCH 6/7] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) 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à From ff8e8a3534e74aa448220936d0145cd263fea0cf Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Thu, 18 Jul 2024 14:19:10 -0600 Subject: [PATCH 7/7] Append to entities array --- custom_components/midea_ac/binary_sensor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/custom_components/midea_ac/binary_sensor.py b/custom_components/midea_ac/binary_sensor.py index a43a90c..472eb49 100644 --- a/custom_components/midea_ac/binary_sensor.py +++ b/custom_components/midea_ac/binary_sensor.py @@ -32,11 +32,11 @@ async def async_setup_entry( # 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,