From cb3a6d16cdc8f99c0983ec7090cb403ce0a5fca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Mind=C3=AAllo=20de=20Andrade?= Date: Wed, 14 Oct 2026 02:04:37 -0300 Subject: [PATCH] Add reset_filter button to mark Midea AC filter clean (#184372) --- homeassistant/components/midea/button.py | 27 +++++++++- homeassistant/components/midea/strings.json | 3 ++ tests/components/midea/conftest.py | 5 ++ .../midea/snapshots/test_button.ambr | 54 ++++++++++++++++++- tests/components/midea/test_button.py | 38 +++++++++++-- 5 files changed, 118 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/midea/button.py b/homeassistant/components/midea/button.py index c1e4795b6d28..12090cc05906 100644 --- a/homeassistant/components/midea/button.py +++ b/homeassistant/components/midea/button.py @@ -6,9 +6,11 @@ from typing import cast, override from midealocal.const import DeviceType from midealocal.device import MideaDevice +from midealocal.devices.ac import MideaACDevice from midealocal.devices.e1 import MideaE1Device from homeassistant.components.button import ButtonEntity, ButtonEntityDescription +from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback @@ -24,8 +26,14 @@ class MideaButtonEntityDescription(ButtonEntityDescription): """Description for a Midea button entity.""" models: list[DeviceType] - supported_models: list[str] press_fn: Callable[[MideaDevice], None] + supported_models: list[str] | None = None + supported_property_name: str | None = None + + +def _ac_reset_filter_press(device: MideaDevice) -> None: + """Reset filter counters.""" + cast(MideaACDevice, device).reset_filter() def _e1_button_press(device: MideaDevice) -> None: @@ -46,6 +54,14 @@ BUTTONS: list[MideaButtonEntityDescription] = [ supported_models=["7600024L"], press_fn=_e1_button_press, ), + MideaButtonEntityDescription( + key="reset_filter", + translation_key="reset_filter", + entity_category=EntityCategory.CONFIG, + models=[DeviceType.AC], + supported_property_name="is_filter_reset_supported", + press_fn=_ac_reset_filter_press, + ), ] @@ -61,7 +77,14 @@ async def async_setup_entry( MideaButton(device, description) for description in BUTTONS if device.device_type in description.models - and device.model in description.supported_models + and ( + description.supported_models is None + or device.model in description.supported_models + ) + and ( + description.supported_property_name is None + or getattr(device, description.supported_property_name, False) + ) ) diff --git a/homeassistant/components/midea/strings.json b/homeassistant/components/midea/strings.json index 195728d248d4..9be46a0436d0 100644 --- a/homeassistant/components/midea/strings.json +++ b/homeassistant/components/midea/strings.json @@ -177,6 +177,9 @@ } }, "button": { + "reset_filter": { + "name": "Reset filter counters" + }, "start": { "name": "Start" } diff --git a/tests/components/midea/conftest.py b/tests/components/midea/conftest.py index 2bbe67215807..b500621b1690 100644 --- a/tests/components/midea/conftest.py +++ b/tests/components/midea/conftest.py @@ -50,6 +50,7 @@ class DummyDevice: self.capabilities: dict[str, Any] = capabilities or {} self._callbacks: list[Callable] = [] self.calls: list[tuple] = [] + self.is_filter_reset_supported = False self.temperature_step = 1 self.raw_hvac_modes = ["off", "auto", "cool", "dry", "heat", "fan_only"] self.raw_fan_modes = ["low", "medium", "high", "auto"] @@ -107,6 +108,10 @@ class DummyDevice: """Record set hvac mode call.""" self.calls.append(("set_raw_hvac_mode", hvac_mode, zone)) + def reset_filter(self) -> None: + """Record filter reset call.""" + self.calls.append(("reset_filter",)) + def start_work(self) -> None: """Record start_work call.""" self.calls.append(("start_work",)) diff --git a/tests/components/midea/snapshots/test_button.ambr b/tests/components/midea/snapshots/test_button.ambr index fddfd6fbe786..b1e44dec37e2 100644 --- a/tests/components/midea/snapshots/test_button.ambr +++ b/tests/components/midea/snapshots/test_button.ambr @@ -1,5 +1,55 @@ # serializer version: 1 -# name: test_button_state_snapshot[button.dishwasher_start-entry] +# name: test_button_state_snapshot[ac][button.air_conditioner_reset_filter_counters-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.air_conditioner_reset_filter_counters', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Reset filter counters', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Reset filter counters', + 'platform': 'midea', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'reset_filter', + 'unique_id': '12345678_reset_filter', + 'unit_of_measurement': None, + }) +# --- +# name: test_button_state_snapshot[ac][button.air_conditioner_reset_filter_counters-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'Air Conditioner Reset filter counters', + }), + 'context': , + 'entity_id': 'button.air_conditioner_reset_filter_counters', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_button_state_snapshot[e1][button.dishwasher_start-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ None, @@ -36,7 +86,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_button_state_snapshot[button.dishwasher_start-state] +# name: test_button_state_snapshot[e1][button.dishwasher_start-state] StateSnapshot({ 'attributes': ReadOnlyDict({ : 'Dishwasher Start', diff --git a/tests/components/midea/test_button.py b/tests/components/midea/test_button.py index d6116fe38101..e07b61e6fb39 100644 --- a/tests/components/midea/test_button.py +++ b/tests/components/midea/test_button.py @@ -35,15 +35,33 @@ def _e1_device(*, model: str = "7600024L") -> DummyDevice: return device +def _ac_device() -> DummyDevice: + device = DummyDevice( + DeviceType.AC, + attributes={ + ACAttributes.power: True, + }, + ) + device.is_filter_reset_supported = True + return device + + @pytest.mark.usefixtures("entity_registry_enabled_by_default") +@pytest.mark.parametrize( + ("device"), + [ + pytest.param(_ac_device(), id="ac"), + pytest.param(_e1_device(), id="e1"), + ], +) async def test_button_state_snapshot( hass: HomeAssistant, mock_config_entry: Callable[[DummyDevice], MockConfigEntry], snapshot: SnapshotAssertion, entity_registry: er.EntityRegistry, + device: DummyDevice, ) -> None: """Test async_setup_entry creates the start button for a supported E1 dishwasher.""" - device = _e1_device() config_entry = mock_config_entry(device) with patch("homeassistant.components.midea._PLATFORMS", [Platform.BUTTON]): await setup_integration(hass, config_entry, device) @@ -51,17 +69,26 @@ async def test_button_state_snapshot( await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) -async def test_button_press_starts_dishwasher( +@pytest.mark.parametrize( + ("device", "entity_name", "expected_call"), + [ + pytest.param(_ac_device(), "reset_filter", ("reset_filter",), id="ac"), + pytest.param(_e1_device(), "start", ("start_work",), id="e1"), + ], +) +async def test_button_press( hass: HomeAssistant, mock_config_entry: Callable[[DummyDevice], MockConfigEntry], + device: DummyDevice, + entity_name: str, + expected_call: tuple, ) -> None: """Test pressing the button calls start_work on the device.""" - device = _e1_device() config_entry = mock_config_entry(device) with patch("homeassistant.components.midea._PLATFORMS", [Platform.BUTTON]): await setup_integration(hass, config_entry, device) - entity_entry = entity_entries(hass, config_entry)[f"{TEST_DEVICE_ID}_start"] + entity_entry = entity_entries(hass, config_entry)[f"{TEST_DEVICE_ID}_{entity_name}"] assert (state := hass.states.get(entity_entry.entity_id)) is not None assert state.state != "unavailable" @@ -73,7 +100,8 @@ async def test_button_press_starts_dishwasher( {ATTR_ENTITY_ID: entity_entry.entity_id}, blocking=True, ) - assert device.calls == [("start_work",)] + assert len(device.calls) == 1 + assert device.calls[0] == expected_call async def test_button_press_raises_when_device_off(