mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add reset_filter button to mark Midea AC filter clean (#184372)
This commit is contained in:
@@ -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)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -177,6 +177,9 @@
|
||||
}
|
||||
},
|
||||
"button": {
|
||||
"reset_filter": {
|
||||
"name": "Reset filter counters"
|
||||
},
|
||||
"start": {
|
||||
"name": "Start"
|
||||
}
|
||||
|
||||
@@ -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",))
|
||||
|
||||
@@ -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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'button.air_conditioner_reset_filter_counters',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Air Conditioner Reset filter counters',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.air_conditioner_reset_filter_counters',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Dishwasher Start',
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user