mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Vesync Display Switch Feature (#137493)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
9e93d1fd7e
commit
82efa0893f
@ -65,6 +65,11 @@
|
||||
"name": "Mist level"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"display": {
|
||||
"name": "Display"
|
||||
}
|
||||
},
|
||||
"select": {
|
||||
"night_light_level": {
|
||||
"name": "Night light level",
|
||||
|
@ -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):
|
||||
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):
|
||||
if not self.entity_description.on_fn(self.device):
|
||||
raise HomeAssistantError("An error occurred while turning on.")
|
||||
|
||||
self.schedule_update_ha_state()
|
||||
|
@ -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"]
|
||||
|
@ -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,
|
||||
|
@ -36,8 +36,53 @@
|
||||
# ---
|
||||
# name: test_switch_state[Air Purifier 131s][entities]
|
||||
list([
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'switch.air_purifier_131s_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'switch.air_purifier_200s_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'switch.air_purifier_400s_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'switch.air_purifier_600s_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.humidifier_200s_display',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'switch.humidifier_200s_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.humidifier_600s_display',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'switch.humidifier_600s_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.smarttowerfan_display',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'switch.smarttowerfan_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_switch_state[Temperature Light][devices]
|
||||
list([
|
||||
DeviceRegistryEntrySnapshot({
|
||||
|
@ -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"
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user