mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Add power switch for Midea air purifiers (#188177)
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <DeviceEntry identifiers=[('midea', '12345678')]>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'switch.air_purifier_child_lock',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Air Purifier Child lock',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.air_purifier_child_lock',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <DeviceEntry identifiers=[('midea', '12345678')]>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.air_purifier_power',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Power',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
|
||||
'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({
|
||||
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'switch',
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Air Purifier Power',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.air_purifier_power',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
|
||||
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user