diff --git a/homeassistant/components/midea/icons.json b/homeassistant/components/midea/icons.json index e23b7129ba94..5b0e88519d73 100644 --- a/homeassistant/components/midea/icons.json +++ b/homeassistant/components/midea/icons.json @@ -94,6 +94,9 @@ "prompt_tone": { "default": "mdi:bullhorn" }, + "pump": { + "default": "mdi:water-pump" + }, "screen_display": { "default": "mdi:monitor" }, diff --git a/homeassistant/components/midea/strings.json b/homeassistant/components/midea/strings.json index e56fe456bee5..356573180aeb 100644 --- a/homeassistant/components/midea/strings.json +++ b/homeassistant/components/midea/strings.json @@ -692,6 +692,9 @@ "prompt_tone": { "name": "Prompt tone" }, + "pump": { + "name": "Pump" + }, "screen_display": { "name": "Screen display" }, diff --git a/homeassistant/components/midea/switch.py b/homeassistant/components/midea/switch.py index f9ae21c5d85a..e2ea81e11f3f 100644 --- a/homeassistant/components/midea/switch.py +++ b/homeassistant/components/midea/switch.py @@ -19,9 +19,16 @@ class MideaSwitchEntityDescription(SwitchEntityDescription): """Description for a Midea switch entity.""" models: list[DeviceType] + capability: str | None = None SWITCHES: list[MideaSwitchEntityDescription] = [ + MideaSwitchEntityDescription( + key="pump", + translation_key="pump", + models=[DeviceType.A1], + capability="pump", + ), MideaSwitchEntityDescription( key="aux_heating", translation_key="aux_heating", @@ -118,6 +125,12 @@ async def async_setup_entry( for description in SWITCHES if device.device_type in description.models and description.key in device.attributes + and ( + description.capability is None + or (getattr(device, "capabilities", None) or {}).get( + description.capability, False + ) + ) ) diff --git a/tests/components/midea/conftest.py b/tests/components/midea/conftest.py index 4aa408e1a33b..2bbe67215807 100644 --- a/tests/components/midea/conftest.py +++ b/tests/components/midea/conftest.py @@ -37,6 +37,7 @@ class DummyDevice: device_type: DeviceType, *, attributes: dict | None = None, + capabilities: dict | None = None, ) -> None: """Initialize fake device.""" self.device_type = device_type @@ -46,6 +47,7 @@ class DummyDevice: self.subtype = TEST_SUBTYPE self.available = False self.attributes = attributes or {} + self.capabilities: dict[str, Any] = capabilities or {} self._callbacks: list[Callable] = [] self.calls: list[tuple] = [] self.temperature_step = 1 diff --git a/tests/components/midea/snapshots/test_switch.ambr b/tests/components/midea/snapshots/test_switch.ambr index dbf7c45a891e..34df594b5eb2 100644 --- a/tests/components/midea/snapshots/test_switch.ambr +++ b/tests/components/midea/snapshots/test_switch.ambr @@ -1,4 +1,54 @@ # serializer version: 1 +# name: test_switch_state_snapshot[a1][switch.dehumidifier_pump-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.dehumidifier_pump', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Pump', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Pump', + 'platform': 'midea', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'pump', + 'unique_id': '12345678_pump', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_state_snapshot[a1][switch.dehumidifier_pump-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'Dehumidifier Pump', + }), + 'context': , + 'entity_id': 'switch.dehumidifier_pump', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- # name: test_switch_state_snapshot[ac][switch.air_conditioner_anion-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ diff --git a/tests/components/midea/test_switch.py b/tests/components/midea/test_switch.py index dc1c51773ba7..a34aeb2f7088 100644 --- a/tests/components/midea/test_switch.py +++ b/tests/components/midea/test_switch.py @@ -4,6 +4,7 @@ from collections.abc import Callable from unittest.mock import patch from midealocal.const import DeviceType +from midealocal.devices.a1 import DeviceAttributes as A1Attributes from midealocal.devices.ac import DeviceAttributes as ACAttributes from midealocal.devices.c3 import DeviceAttributes as C3Attributes from midealocal.devices.cc import DeviceAttributes as CCAttributes @@ -102,6 +103,14 @@ async def _assert_service_call( ), id="c3", ), + pytest.param( + DummyDevice( + DeviceType.A1, + attributes={A1Attributes.pump: False}, + capabilities={"pump": True}, + ), + id="a1", + ), pytest.param( DummyDevice( DeviceType.CC, @@ -228,6 +237,65 @@ async def test_child_lock_switch_created_and_services( ) +async def test_a1_pump_services( + hass: HomeAssistant, + mock_config_entry: Callable[[DummyDevice], MockConfigEntry], +) -> None: + """Test the A1 dehumidifier pump switch.""" + device = DummyDevice( + DeviceType.A1, + attributes={A1Attributes.pump: False}, + capabilities={"pump": True}, + ) + 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}_pump"] + + assert (state := hass.states.get(entity_entry.entity_id)) is not None + assert state.state == "off" + + await _assert_service_call( + hass, + entity_entry.entity_id, + SERVICE_TURN_ON, + [("set_attribute", A1Attributes.pump, True)], + device, + ) + await hass.async_block_till_done() + assert (state := hass.states.get(entity_entry.entity_id)) is not None + assert state.state == "on" + + await _assert_service_call( + hass, + entity_entry.entity_id, + SERVICE_TURN_OFF, + [("set_attribute", A1Attributes.pump, False)], + device, + ) + await hass.async_block_till_done() + assert (state := hass.states.get(entity_entry.entity_id)) is not None + assert state.state == "off" + + +async def test_a1_pump_not_created_without_capability( + hass: HomeAssistant, + mock_config_entry: Callable[[DummyDevice], MockConfigEntry], +) -> None: + """Test that unsupported A1 pump switches are not created.""" + device = DummyDevice( + DeviceType.A1, + attributes={A1Attributes.pump: False}, + capabilities={"pump": False}, + ) + 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}_pump" not in entity_entries(hass, config_entry) + + async def test_switch_unknown_when_attribute_becomes_non_bool( hass: HomeAssistant, mock_config_entry: Callable[[DummyDevice], MockConfigEntry],