From 65e0414ff284d435d1019196d12bfa9e8d2df06d Mon Sep 17 00:00:00 2001 From: vrpolice <33617621+vrpolice@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:36:39 +0800 Subject: [PATCH] Add power switch for Midea air purifiers (#188177) --- homeassistant/components/midea/switch.py | 2 +- .../midea/snapshots/test_switch.ambr | 101 ++++++++++++++++++ tests/components/midea/test_switch.py | 99 ++++++++++++++--- 3 files changed, 184 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/midea/switch.py b/homeassistant/components/midea/switch.py index e3f45fb4412d..e9040cc74568 100644 --- a/homeassistant/components/midea/switch.py +++ b/homeassistant/components/midea/switch.py @@ -148,7 +148,7 @@ SWITCHES: list[MideaSwitchEntityDescription] = [ MideaSwitchEntityDescription( key="power", translation_key="power", - models=[DeviceType.DA, DeviceType.DB, DeviceType.DC], + models=[DeviceType.DA, DeviceType.DB, DeviceType.DC, DeviceType.FC], device_class=SwitchDeviceClass.SWITCH, ), MideaSwitchEntityDescription( diff --git a/tests/components/midea/snapshots/test_switch.ambr b/tests/components/midea/snapshots/test_switch.ambr index 58a7d16eaca7..01fcd9c04440 100644 --- a/tests/components/midea/snapshots/test_switch.ambr +++ b/tests/components/midea/snapshots/test_switch.ambr @@ -1511,3 +1511,104 @@ 'state': 'off', }) # --- +# name: test_switch_state_snapshot[fc][switch.air_purifier_child_lock-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': 'switch', + 'entity_category': , + 'entity_id': 'switch.air_purifier_child_lock', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Child lock', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Child lock', + 'platform': 'midea', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'child_lock', + 'unique_id': '12345678_child_lock', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_state_snapshot[fc][switch.air_purifier_child_lock-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'Air Purifier Child lock', + }), + 'context': , + 'entity_id': 'switch.air_purifier_child_lock', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switch_state_snapshot[fc][switch.air_purifier_power-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': 'switch', + 'entity_category': None, + 'entity_id': 'switch.air_purifier_power', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Power', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power', + 'platform': 'midea', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'power', + 'unique_id': '12345678_power', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_state_snapshot[fc][switch.air_purifier_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'switch', + : 'Air Purifier Power', + }), + 'context': , + 'entity_id': 'switch.air_purifier_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/midea/test_switch.py b/tests/components/midea/test_switch.py index 4442d0c43f6b..fbf54fb05b7f 100644 --- a/tests/components/midea/test_switch.py +++ b/tests/components/midea/test_switch.py @@ -13,6 +13,7 @@ from midealocal.devices.da import DeviceAttributes as DAAttributes from midealocal.devices.db import DeviceAttributes as DBAttributes from midealocal.devices.dc import DeviceAttributes as DCAttributes from midealocal.devices.e2 import DeviceAttributes as E2Attributes +from midealocal.devices.fc import DeviceAttributes as FCAttributes from midealocal.exceptions import SocketException import pytest from pytest_homeassistant.common import ( @@ -195,6 +196,17 @@ def _set_attribute_calls(device: DummyDevice) -> list[tuple]: ), id="e2", ), + pytest.param( + DummyDevice( + DeviceType.FC, + attributes={ + FCAttributes.power: True, + FCAttributes.mode: "auto", + FCAttributes.child_lock: False, + }, + ), + id="fc", + ), ], ) @pytest.mark.usefixtures("entity_registry_enabled_by_default") @@ -213,44 +225,84 @@ async def test_switch_state_snapshot( await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) -async def test_ac_switch_services( +@pytest.mark.parametrize( + ("device", "attribute", "expected_state"), + [ + pytest.param( + DummyDevice( + DeviceType.AC, + attributes={ + ACAttributes.power: True, + ACAttributes.mode: 1, + ACAttributes.target_temperature: 22.0, + ACAttributes.indoor_temperature: 21.0, + ACAttributes.aux_heating: False, + }, + ), + ACAttributes.aux_heating, + STATE_OFF, + id="ac", + ), + pytest.param( + DummyDevice( + DeviceType.FC, + attributes={FCAttributes.power: False, FCAttributes.mode: "auto"}, + ), + FCAttributes.power, + STATE_OFF, + id="fc_off", + ), + pytest.param( + DummyDevice( + DeviceType.FC, + attributes={FCAttributes.power: True, FCAttributes.mode: "auto"}, + ), + FCAttributes.power, + STATE_ON, + id="fc_on", + ), + ], +) +async def test_switch_services( hass: HomeAssistant, mock_config_entry: Callable[[DummyDevice], MockConfigEntry], + device: DummyDevice, + attribute: str, + expected_state: str, ) -> None: - """Test AC switch service calls reach the device.""" - device = DummyDevice( - DeviceType.AC, - attributes={ - ACAttributes.power: True, - ACAttributes.mode: 1, - ACAttributes.target_temperature: 22.0, - ACAttributes.indoor_temperature: 21.0, - ACAttributes.aux_heating: False, - }, - ) + """Test switch services update only the selected device attribute.""" + original_attributes = device.attributes.copy() config_entry = mock_config_entry(device) with patch("homeassistant.components.midea._PLATFORMS", [Platform.SWITCH]): await setup_integration(hass, config_entry, device) - entity_entry = entity_entries(hass, config_entry)[f"{TEST_DEVICE_ID}_aux_heating"] - + entity_entry = entity_entries(hass, config_entry)[f"{TEST_DEVICE_ID}_{attribute}"] assert (state := hass.states.get(entity_entry.entity_id)) is not None - assert state.state == "off" + assert state.state == expected_state await _assert_service_call( hass, entity_entry.entity_id, SERVICE_TURN_ON, - [("set_attribute", ACAttributes.aux_heating, True)], + [("set_attribute", attribute, True)], device, ) + await hass.async_block_till_done() + assert device.attributes == {**original_attributes, attribute: True} + assert (state := hass.states.get(entity_entry.entity_id)) is not None + assert state.state == STATE_ON + await _assert_service_call( hass, entity_entry.entity_id, SERVICE_TURN_OFF, - [("set_attribute", ACAttributes.aux_heating, False)], + [("set_attribute", attribute, False)], device, ) + await hass.async_block_till_done() + assert device.attributes == {**original_attributes, attribute: False} + assert (state := hass.states.get(entity_entry.entity_id)) is not None + assert state.state == STATE_OFF @pytest.mark.parametrize( @@ -578,6 +630,19 @@ async def test_washing_machine_switch_services( assert state.state == STATE_OFF +async def test_air_purifier_power_switch_requires_attribute( + hass: HomeAssistant, + mock_config_entry: Callable[[DummyDevice], MockConfigEntry], +) -> None: + """Test a purifier without a power attribute gets no power switch.""" + device = DummyDevice(DeviceType.FC, attributes={FCAttributes.mode: "auto"}) + config_entry = mock_config_entry(device) + with patch("homeassistant.components.midea._PLATFORMS", [Platform.SWITCH]): + await setup_integration(hass, config_entry, device) + + assert f"{TEST_DEVICE_ID}_power" not in entity_entries(hass, config_entry) + + async def test_a1_pump_services( hass: HomeAssistant, mock_config_entry: Callable[[DummyDevice], MockConfigEntry],