From 6ca17fa60485c13b8ff3d02bc7b2c05bf3df450f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Mind=C3=AAllo=20de=20Andrade?= Date: Tue, 6 Oct 2026 02:20:06 -0300 Subject: [PATCH] Add washing machine controls to Midea (#184228) --- homeassistant/components/midea/icons.json | 3 + homeassistant/components/midea/strings.json | 6 + homeassistant/components/midea/switch.py | 18 +- .../midea/snapshots/test_switch.ambr | 306 ++++++++++++++++++ tests/components/midea/test_switch.py | 100 +++++- 5 files changed, 424 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/midea/icons.json b/homeassistant/components/midea/icons.json index a80e1afe3735..4301f38bd48f 100644 --- a/homeassistant/components/midea/icons.json +++ b/homeassistant/components/midea/icons.json @@ -103,6 +103,9 @@ "pump": { "default": "mdi:water-pump" }, + "running": { + "default": "mdi:play" + }, "screen_display": { "default": "mdi:monitor" }, diff --git a/homeassistant/components/midea/strings.json b/homeassistant/components/midea/strings.json index 8a666bd15e3c..6a8ba527704e 100644 --- a/homeassistant/components/midea/strings.json +++ b/homeassistant/components/midea/strings.json @@ -715,12 +715,18 @@ "out_silent": { "name": "Outdoor silent mode" }, + "power": { + "name": "Power" + }, "prompt_tone": { "name": "Prompt tone" }, "pump": { "name": "Pump" }, + "running": { + "name": "Running" + }, "screen_display": { "name": "Screen display" }, diff --git a/homeassistant/components/midea/switch.py b/homeassistant/components/midea/switch.py index c2e62326fa79..46590897507c 100644 --- a/homeassistant/components/midea/switch.py +++ b/homeassistant/components/midea/switch.py @@ -5,7 +5,11 @@ from typing import Any, override from midealocal.const import DeviceType -from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription +from homeassistant.components.switch import ( + SwitchDeviceClass, + SwitchEntity, + SwitchEntityDescription, +) from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback @@ -124,6 +128,18 @@ SWITCHES: list[MideaSwitchEntityDescription] = [ models=[DeviceType.CC], entity_category=EntityCategory.CONFIG, ), + MideaSwitchEntityDescription( + key="power", + translation_key="power", + models=[DeviceType.DA, DeviceType.DB, DeviceType.DC], + device_class=SwitchDeviceClass.SWITCH, + ), + MideaSwitchEntityDescription( + key="start", + translation_key="running", + models=[DeviceType.DA, DeviceType.DB, DeviceType.DC], + device_class=SwitchDeviceClass.SWITCH, + ), MideaSwitchEntityDescription( key="ai_switch", translation_key="ai_switch", diff --git a/tests/components/midea/snapshots/test_switch.ambr b/tests/components/midea/snapshots/test_switch.ambr index db1d59821b33..2cd8ef8f3668 100644 --- a/tests/components/midea/snapshots/test_switch.ambr +++ b/tests/components/midea/snapshots/test_switch.ambr @@ -849,6 +849,210 @@ 'state': 'on', }) # --- +# name: test_switch_state_snapshot[da][switch.top_load_washer_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.top_load_washer_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[da][switch.top_load_washer_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'switch', + : 'Top Load Washer Power', + }), + 'context': , + 'entity_id': 'switch.top_load_washer_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switch_state_snapshot[da][switch.top_load_washer_running-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.top_load_washer_running', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Running', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Running', + 'platform': 'midea', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'running', + 'unique_id': '12345678_start', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_state_snapshot[da][switch.top_load_washer_running-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'switch', + : 'Top Load Washer Running', + }), + 'context': , + 'entity_id': 'switch.top_load_washer_running', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switch_state_snapshot[db][switch.front_load_washer_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.front_load_washer_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[db][switch.front_load_washer_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'switch', + : 'Front Load Washer Power', + }), + 'context': , + 'entity_id': 'switch.front_load_washer_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switch_state_snapshot[db][switch.front_load_washer_running-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.front_load_washer_running', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Running', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Running', + 'platform': 'midea', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'running', + 'unique_id': '12345678_start', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_state_snapshot[db][switch.front_load_washer_running-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'switch', + : 'Front Load Washer Running', + }), + 'context': , + 'entity_id': 'switch.front_load_washer_running', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- # name: test_switch_state_snapshot[dc][switch.clothes_dryer_ai_switch-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -899,3 +1103,105 @@ 'state': 'off', }) # --- +# name: test_switch_state_snapshot[dc][switch.clothes_dryer_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.clothes_dryer_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[dc][switch.clothes_dryer_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'switch', + : 'Clothes Dryer Power', + }), + 'context': , + 'entity_id': 'switch.clothes_dryer_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switch_state_snapshot[dc][switch.clothes_dryer_running-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.clothes_dryer_running', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Running', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Running', + 'platform': 'midea', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'running', + 'unique_id': '12345678_start', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_state_snapshot[dc][switch.clothes_dryer_running-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'switch', + : 'Clothes Dryer Running', + }), + 'context': , + 'entity_id': 'switch.clothes_dryer_running', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/midea/test_switch.py b/tests/components/midea/test_switch.py index ef3851810c98..1416dea39330 100644 --- a/tests/components/midea/test_switch.py +++ b/tests/components/midea/test_switch.py @@ -9,6 +9,8 @@ from midealocal.devices.ac import DeviceAttributes as ACAttributes from midealocal.devices.c3 import DeviceAttributes as C3Attributes from midealocal.devices.cc import DeviceAttributes as CCAttributes from midealocal.devices.cf import DeviceAttributes as CFAttributes +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.exceptions import SocketException import pytest @@ -19,7 +21,13 @@ from homeassistant.components.switch import ( SERVICE_TURN_OFF, SERVICE_TURN_ON, ) -from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.const import ( + ATTR_ENTITY_ID, + STATE_OFF, + STATE_ON, + STATE_UNKNOWN, + Platform, +) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er @@ -136,7 +144,28 @@ async def _assert_service_call( id="c2", ), pytest.param( - DummyDevice(DeviceType.DC, attributes={DCAttributes.ai_switch: False}), + DummyDevice( + DeviceType.DA, + attributes={DAAttributes.power: False, DAAttributes.start: False}, + ), + id="da", + ), + pytest.param( + DummyDevice( + DeviceType.DB, + attributes={DBAttributes.power: False, DBAttributes.start: False}, + ), + id="db", + ), + pytest.param( + DummyDevice( + DeviceType.DC, + attributes={ + DCAttributes.power: False, + DCAttributes.start: False, + DCAttributes.ai_switch: False, + }, + ), id="dc", ), ], @@ -276,6 +305,61 @@ async def test_child_lock_switch_created_and_services( ) +@pytest.mark.parametrize( + ("device_type", "switch"), + [ + (DeviceType.DA, "power"), + (DeviceType.DA, "start"), + (DeviceType.DB, "power"), + (DeviceType.DB, "start"), + (DeviceType.DC, "power"), + (DeviceType.DC, "start"), + ], +) +async def test_washing_machine_switch_services( + hass: HomeAssistant, + mock_config_entry: Callable[[DummyDevice], MockConfigEntry], + device_type: DeviceType, + switch: str, +) -> None: + """Test the washing machine start switch service calls reach the device.""" + + device = DummyDevice( + device_type, + attributes={"power": False, "start": False}, + ) + 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}_{switch}"] + + assert (state := hass.states.get(entity_entry.entity_id)) is not None + assert state.state == STATE_OFF + + await _assert_service_call( + hass, + entity_entry.entity_id, + SERVICE_TURN_ON, + [("set_attribute", switch, True)], + device, + ) + await hass.async_block_till_done() + 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", switch, False)], + device, + ) + await hass.async_block_till_done() + assert (state := hass.states.get(entity_entry.entity_id)) is not None + assert state.state == STATE_OFF + + async def test_a1_pump_services( hass: HomeAssistant, mock_config_entry: Callable[[DummyDevice], MockConfigEntry], @@ -293,7 +377,7 @@ async def test_a1_pump_services( entity_entry = entity_entries(hass, config_entry)[f"{TEST_DEVICE_ID}_pump"] assert (state := hass.states.get(entity_entry.entity_id)) is not None - assert state.state == "off" + assert state.state == STATE_OFF await _assert_service_call( hass, @@ -304,7 +388,7 @@ async def test_a1_pump_services( ) await hass.async_block_till_done() assert (state := hass.states.get(entity_entry.entity_id)) is not None - assert state.state == "on" + assert state.state == STATE_ON await _assert_service_call( hass, @@ -315,7 +399,7 @@ async def test_a1_pump_services( ) await hass.async_block_till_done() assert (state := hass.states.get(entity_entry.entity_id)) is not None - assert state.state == "off" + assert state.state == STATE_OFF async def test_a1_pump_not_created_without_capability( @@ -356,21 +440,21 @@ async def test_switch_unknown_when_attribute_becomes_non_bool( entity_entry = entity_entries(hass, config_entry)[f"{TEST_DEVICE_ID}_aux_heating"] assert (state := hass.states.get(entity_entry.entity_id)) - assert state.state == "off" + assert state.state == STATE_OFF device.attributes[ACAttributes.aux_heating] = True device.notify_update({ACAttributes.aux_heating: True}) await hass.async_block_till_done() assert (state := hass.states.get(entity_entry.entity_id)) - assert state.state == "on" + assert state.state == STATE_ON device.attributes[ACAttributes.aux_heating] = None device.notify_update({ACAttributes.aux_heating: None}) await hass.async_block_till_done() assert (state := hass.states.get(entity_entry.entity_id)) - assert state.state == "unknown" + assert state.state == STATE_UNKNOWN async def test_switch_turn_on_raises_on_device_communication_error(