diff --git a/homeassistant/components/vesync/strings.json b/homeassistant/components/vesync/strings.json index 9b63bf3e614..241ccfa0af0 100644 --- a/homeassistant/components/vesync/strings.json +++ b/homeassistant/components/vesync/strings.json @@ -65,6 +65,11 @@ "name": "Mist level" } }, + "switch": { + "display": { + "name": "Display" + } + }, "select": { "night_light_level": { "name": "Night light level", diff --git a/homeassistant/components/vesync/switch.py b/homeassistant/components/vesync/switch.py index 3e8deedb4ad..06fbd3606bd 100644 --- a/homeassistant/components/vesync/switch.py +++ b/homeassistant/components/vesync/switch.py @@ -14,10 +14,11 @@ from homeassistant.components.switch import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .common import is_outlet, is_wall_switch +from .common import is_outlet, is_wall_switch, rgetattr from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY from .coordinator import VeSyncDataCoordinator from .entity import VeSyncBaseEntity @@ -45,6 +46,14 @@ SENSOR_DESCRIPTIONS: Final[tuple[VeSyncSwitchEntityDescription, ...]] = ( on_fn=lambda device: device.turn_on(), off_fn=lambda device: device.turn_off(), ), + VeSyncSwitchEntityDescription( + key="display", + is_on=lambda device: device.display_state, + exists_fn=lambda device: rgetattr(device, "display_state") is not None, + translation_key="display", + on_fn=lambda device: device.turn_on_display(), + off_fn=lambda device: device.turn_off_display(), + ), ) @@ -111,10 +120,14 @@ class VeSyncSwitchEntity(SwitchEntity, VeSyncBaseEntity): def turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" - if self.entity_description.off_fn(self.device): - self.schedule_update_ha_state() + if not self.entity_description.off_fn(self.device): + raise HomeAssistantError("An error occurred while turning off.") + + self.schedule_update_ha_state() def turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" - if self.entity_description.on_fn(self.device): - self.schedule_update_ha_state() + if not self.entity_description.on_fn(self.device): + raise HomeAssistantError("An error occurred while turning on.") + + self.schedule_update_ha_state() diff --git a/tests/components/vesync/common.py b/tests/components/vesync/common.py index 18cb094563e..5795c977120 100644 --- a/tests/components/vesync/common.py +++ b/tests/components/vesync/common.py @@ -15,6 +15,8 @@ ENTITY_HUMIDIFIER_MIST_LEVEL = "number.humidifier_200s_mist_level" ENTITY_HUMIDIFIER_HUMIDITY = "sensor.humidifier_200s_humidity" ENTITY_HUMIDIFIER_300S_NIGHT_LIGHT_SELECT = "select.humidifier_300s_night_light_level" +ENTITY_SWITCH_DISPLAY = "switch.humidifier_200s_display" + ALL_DEVICES = load_json_object_fixture("vesync-devices.json", DOMAIN) ALL_DEVICE_NAMES: list[str] = [ dev["deviceName"] for dev in ALL_DEVICES["result"]["list"] diff --git a/tests/components/vesync/snapshots/test_diagnostics.ambr b/tests/components/vesync/snapshots/test_diagnostics.ambr index 407e18d65b6..aa55a9be3cb 100644 --- a/tests/components/vesync/snapshots/test_diagnostics.ambr +++ b/tests/components/vesync/snapshots/test_diagnostics.ambr @@ -290,6 +290,30 @@ }), 'unit_of_measurement': '%', }), + dict({ + 'device_class': None, + 'disabled': False, + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.fan_display', + 'icon': None, + 'name': None, + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'state': dict({ + 'attributes': dict({ + 'friendly_name': 'Fan Display', + }), + 'entity_id': 'switch.fan_display', + 'last_changed': str, + 'last_reported': str, + 'last_updated': str, + 'state': 'unavailable', + }), + 'unit_of_measurement': None, + }), ]), 'name': 'Fan', 'name_by_user': None, diff --git a/tests/components/vesync/snapshots/test_switch.ambr b/tests/components/vesync/snapshots/test_switch.ambr index 1faed941338..f25aaf3d51b 100644 --- a/tests/components/vesync/snapshots/test_switch.ambr +++ b/tests/components/vesync/snapshots/test_switch.ambr @@ -36,8 +36,53 @@ # --- # name: test_switch_state[Air Purifier 131s][entities] list([ + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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_131s_display', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'platform': 'vesync', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'display', + 'unique_id': 'air-purifier-display', + 'unit_of_measurement': None, + }), ]) # --- +# name: test_switch_state[Air Purifier 131s][switch.air_purifier_131s_display] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Air Purifier 131s Display', + }), + 'context': , + 'entity_id': 'switch.air_purifier_131s_display', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_switch_state[Air Purifier 200s][devices] list([ DeviceRegistryEntrySnapshot({ @@ -75,8 +120,53 @@ # --- # name: test_switch_state[Air Purifier 200s][entities] list([ + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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_200s_display', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'platform': 'vesync', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'display', + 'unique_id': 'asd_sdfKIHG7IJHGwJGJ7GJ_ag5h3G55-display', + 'unit_of_measurement': None, + }), ]) # --- +# name: test_switch_state[Air Purifier 200s][switch.air_purifier_200s_display] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Air Purifier 200s Display', + }), + 'context': , + 'entity_id': 'switch.air_purifier_200s_display', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_switch_state[Air Purifier 400s][devices] list([ DeviceRegistryEntrySnapshot({ @@ -114,8 +204,53 @@ # --- # name: test_switch_state[Air Purifier 400s][entities] list([ + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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_400s_display', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'platform': 'vesync', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'display', + 'unique_id': '400s-purifier-display', + 'unit_of_measurement': None, + }), ]) # --- +# name: test_switch_state[Air Purifier 400s][switch.air_purifier_400s_display] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Air Purifier 400s Display', + }), + 'context': , + 'entity_id': 'switch.air_purifier_400s_display', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_switch_state[Air Purifier 600s][devices] list([ DeviceRegistryEntrySnapshot({ @@ -153,8 +288,53 @@ # --- # name: test_switch_state[Air Purifier 600s][entities] list([ + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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_600s_display', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'platform': 'vesync', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'display', + 'unique_id': '600s-purifier-display', + 'unit_of_measurement': None, + }), ]) # --- +# name: test_switch_state[Air Purifier 600s][switch.air_purifier_600s_display] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Air Purifier 600s Display', + }), + 'context': , + 'entity_id': 'switch.air_purifier_600s_display', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_switch_state[Dimmable Light][devices] list([ DeviceRegistryEntrySnapshot({ @@ -270,8 +450,53 @@ # --- # name: test_switch_state[Humidifier 200s][entities] list([ + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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.humidifier_200s_display', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'platform': 'vesync', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'display', + 'unique_id': '200s-humidifier4321-display', + 'unit_of_measurement': None, + }), ]) # --- +# name: test_switch_state[Humidifier 200s][switch.humidifier_200s_display] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Humidifier 200s Display', + }), + 'context': , + 'entity_id': 'switch.humidifier_200s_display', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_switch_state[Humidifier 600S][devices] list([ DeviceRegistryEntrySnapshot({ @@ -309,8 +534,53 @@ # --- # name: test_switch_state[Humidifier 600S][entities] list([ + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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.humidifier_600s_display', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'platform': 'vesync', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'display', + 'unique_id': '600s-humidifier-display', + 'unit_of_measurement': None, + }), ]) # --- +# name: test_switch_state[Humidifier 600S][switch.humidifier_600s_display] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Humidifier 600S Display', + }), + 'context': , + 'entity_id': 'switch.humidifier_600s_display', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_switch_state[Outlet][devices] list([ DeviceRegistryEntrySnapshot({ @@ -433,8 +703,53 @@ # --- # name: test_switch_state[SmartTowerFan][entities] list([ + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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.smarttowerfan_display', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Display', + 'platform': 'vesync', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'display', + 'unique_id': 'smarttowerfan-display', + 'unit_of_measurement': None, + }), ]) # --- +# name: test_switch_state[SmartTowerFan][switch.smarttowerfan_display] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SmartTowerFan Display', + }), + 'context': , + 'entity_id': 'switch.smarttowerfan_display', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- # name: test_switch_state[Temperature Light][devices] list([ DeviceRegistryEntrySnapshot({ diff --git a/tests/components/vesync/test_init.py b/tests/components/vesync/test_init.py index 31df2418b3d..d1e76174ea0 100644 --- a/tests/components/vesync/test_init.py +++ b/tests/components/vesync/test_init.py @@ -163,11 +163,11 @@ async def test_migrate_config_entry( assert migrated_humidifer is not None assert migrated_humidifer.unique_id == "humidifer" - # Assert that only one entity exists in the switch domain + # Assert that entity exists in the switch domain switch_entities = [ e for e in entity_registry.entities.values() if e.domain == "switch" ] - assert len(switch_entities) == 1 + assert len(switch_entities) == 2 humidifer_entities = [ e for e in entity_registry.entities.values() if e.domain == "humidifer" diff --git a/tests/components/vesync/test_switch.py b/tests/components/vesync/test_switch.py index 111f2b80960..e5d5986b364 100644 --- a/tests/components/vesync/test_switch.py +++ b/tests/components/vesync/test_switch.py @@ -1,17 +1,24 @@ """Tests for the switch module.""" +from contextlib import nullcontext +from unittest.mock import patch + import pytest import requests_mock from syrupy import SnapshotAssertion from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er -from .common import ALL_DEVICE_NAMES, mock_devices_response +from .common import ALL_DEVICE_NAMES, ENTITY_SWITCH_DISPLAY, mock_devices_response from tests.common import MockConfigEntry +NoException = nullcontext() + @pytest.mark.parametrize("device_name", ALL_DEVICE_NAMES) async def test_switch_state( @@ -49,3 +56,72 @@ async def test_switch_state( # Check states for entity in entities: assert hass.states.get(entity.entity_id) == snapshot(name=entity.entity_id) + + +@pytest.mark.parametrize( + ("action", "command"), + [ + (SERVICE_TURN_ON, "pyvesync.vesyncfan.VeSyncHumid200300S.turn_on_display"), + (SERVICE_TURN_OFF, "pyvesync.vesyncfan.VeSyncHumid200300S.turn_off_display"), + ], +) +async def test_turn_on_off_display_success( + hass: HomeAssistant, + humidifier_config_entry: MockConfigEntry, + action: str, + command: str, +) -> None: + """Test switch turn on and off command with success response.""" + + with ( + patch( + command, + return_value=True, + ) as method_mock, + patch( + "homeassistant.components.vesync.switch.VeSyncSwitchEntity.schedule_update_ha_state" + ) as update_mock, + ): + await hass.services.async_call( + SWITCH_DOMAIN, + action, + {ATTR_ENTITY_ID: ENTITY_SWITCH_DISPLAY}, + blocking=True, + ) + + await hass.async_block_till_done() + method_mock.assert_called_once() + update_mock.assert_called_once() + + +@pytest.mark.parametrize( + ("action", "command"), + [ + (SERVICE_TURN_ON, "pyvesync.vesyncfan.VeSyncHumid200300S.turn_on_display"), + (SERVICE_TURN_OFF, "pyvesync.vesyncfan.VeSyncHumid200300S.turn_off_display"), + ], +) +async def test_turn_on_off_display_raises_error( + hass: HomeAssistant, + humidifier_config_entry: MockConfigEntry, + action: str, + command: str, +) -> None: + """Test switch turn on and off command raises HomeAssistantError.""" + + with ( + patch( + command, + return_value=False, + ) as method_mock, + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + SWITCH_DOMAIN, + action, + {ATTR_ENTITY_ID: ENTITY_SWITCH_DISPLAY}, + blocking=True, + ) + + await hass.async_block_till_done() + method_mock.assert_called_once()