Skip to content

Commit 9516502

Browse files
authored
Adjust vesync to follow action-setup (home-assistant#157795)
1 parent 7c71c03 commit 9516502

File tree

4 files changed

+137
-49
lines changed

4 files changed

+137
-49
lines changed

homeassistant/components/vesync/__init__.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77

88
from homeassistant.config_entries import ConfigEntry
99
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
10-
from homeassistant.core import HomeAssistant, ServiceCall
10+
from homeassistant.core import HomeAssistant
1111
from homeassistant.exceptions import ConfigEntryAuthFailed
12-
from homeassistant.helpers import entity_registry as er
12+
from homeassistant.helpers import config_validation as cv, entity_registry as er
1313
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1414
from homeassistant.helpers.device_registry import DeviceEntry
15-
from homeassistant.helpers.dispatcher import async_dispatcher_send
15+
from homeassistant.helpers.typing import ConfigType
1616

17-
from .const import DOMAIN, SERVICE_UPDATE_DEVS, VS_COORDINATOR, VS_MANAGER
17+
from .const import DOMAIN, VS_COORDINATOR, VS_MANAGER
1818
from .coordinator import VeSyncDataCoordinator
19+
from .services import async_setup_services
20+
21+
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
1922

2023
PLATFORMS = [
2124
Platform.BINARY_SENSOR,
@@ -32,6 +35,14 @@
3235
_LOGGER = logging.getLogger(__name__)
3336

3437

38+
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
39+
"""Set up my integration."""
40+
41+
async_setup_services(hass)
42+
43+
return True
44+
45+
3546
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
3647
"""Set up Vesync as config entry."""
3748
username = config_entry.data[CONF_USERNAME]
@@ -62,22 +73,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
6273

6374
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
6475

65-
async def async_new_device_discovery(service: ServiceCall) -> None:
66-
"""Discover and add new devices."""
67-
manager = hass.data[DOMAIN][VS_MANAGER]
68-
known_devices = list(manager.devices)
69-
await manager.get_devices()
70-
new_devices = [
71-
device for device in manager.devices if device not in known_devices
72-
]
73-
74-
if new_devices:
75-
async_dispatcher_send(hass, "vesync_new_devices", new_devices)
76-
77-
hass.services.async_register(
78-
DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
79-
)
80-
8176
return True
8277

8378

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Support for VeSync Services."""
2+
3+
from homeassistant.config_entries import ConfigEntryState
4+
from homeassistant.core import HomeAssistant, ServiceCall, callback
5+
from homeassistant.exceptions import ServiceValidationError
6+
from homeassistant.helpers.dispatcher import async_dispatcher_send
7+
8+
from .const import DOMAIN, SERVICE_UPDATE_DEVS, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
9+
10+
11+
@callback
12+
def async_setup_services(hass: HomeAssistant) -> None:
13+
"""Handle for services."""
14+
15+
hass.services.async_register(
16+
DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
17+
)
18+
19+
20+
async def async_new_device_discovery(call: ServiceCall) -> None:
21+
"""Discover and add new devices."""
22+
23+
entries = call.hass.config_entries.async_entries(DOMAIN)
24+
entry = entries[0] if entries else None
25+
26+
if not entry:
27+
raise ServiceValidationError("Entry not found")
28+
if entry.state is not ConfigEntryState.LOADED:
29+
raise ServiceValidationError("Entry not loaded")
30+
manager = call.hass.data[DOMAIN][VS_MANAGER]
31+
known_devices = list(manager.devices)
32+
await manager.get_devices()
33+
new_devices = [device for device in manager.devices if device not in known_devices]
34+
35+
if new_devices:
36+
async_dispatcher_send(call.hass, VS_DISCOVERY.format(VS_DEVICES), new_devices)

tests/components/vesync/test_init.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from pyvesync.utils.errors import VeSyncLoginError
77

88
from homeassistant.components.vesync import (
9-
SERVICE_UPDATE_DEVS,
109
async_remove_config_entry_device,
1110
async_setup_entry,
1211
)
@@ -91,34 +90,6 @@ async def test_async_setup_entry__loads_fans(
9190
assert list(hass.data[DOMAIN][VS_MANAGER].devices) == [fan]
9291

9392

94-
async def test_async_new_device_discovery(
95-
hass: HomeAssistant, config_entry: ConfigEntry, manager: VeSync, fan, humidifier
96-
) -> None:
97-
"""Test new device discovery."""
98-
99-
assert await hass.config_entries.async_setup(config_entry.entry_id)
100-
# Assert platforms loaded
101-
await hass.async_block_till_done()
102-
assert config_entry.state is ConfigEntryState.LOADED
103-
assert not hass.data[DOMAIN][VS_MANAGER].devices
104-
105-
# Mock discovery of new fan which would get added to VS_DEVICES.
106-
manager._dev_list["fans"].append(fan)
107-
await hass.services.async_call(DOMAIN, SERVICE_UPDATE_DEVS, {}, blocking=True)
108-
109-
assert manager.get_devices.call_count == 1
110-
assert hass.data[DOMAIN][VS_MANAGER] == manager
111-
assert list(hass.data[DOMAIN][VS_MANAGER].devices) == [fan]
112-
113-
# Mock discovery of new humidifier which would invoke discovery in all platforms.
114-
manager._dev_list["humidifiers"].append(humidifier)
115-
await hass.services.async_call(DOMAIN, SERVICE_UPDATE_DEVS, {}, blocking=True)
116-
117-
assert manager.get_devices.call_count == 2
118-
assert hass.data[DOMAIN][VS_MANAGER] == manager
119-
assert list(hass.data[DOMAIN][VS_MANAGER].devices) == [fan, humidifier]
120-
121-
12293
async def test_migrate_config_entry(
12394
hass: HomeAssistant,
12495
switch_old_id_config_entry: MockConfigEntry,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""Tests for VeSync services."""
2+
3+
from unittest.mock import AsyncMock
4+
5+
import pytest
6+
from pyvesync import VeSync
7+
8+
from homeassistant.components.vesync import async_setup
9+
from homeassistant.components.vesync.const import (
10+
DOMAIN,
11+
SERVICE_UPDATE_DEVS,
12+
VS_MANAGER,
13+
)
14+
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
15+
from homeassistant.core import HomeAssistant
16+
from homeassistant.exceptions import ServiceValidationError
17+
from homeassistant.helpers import entity_registry as er
18+
19+
20+
async def test_async_new_device_discovery_no_entry(
21+
hass: HomeAssistant,
22+
) -> None:
23+
"""Service should raise when no config entry exists."""
24+
25+
# Ensure the integration is set up so the service is registered
26+
assert await async_setup(hass, {})
27+
28+
# No entries for the domain, service should raise
29+
with pytest.raises(ServiceValidationError, match="Entry not found"):
30+
await hass.services.async_call("vesync", SERVICE_UPDATE_DEVS, {}, blocking=True)
31+
32+
33+
async def test_async_new_device_discovery_entry_not_loaded(
34+
hass: HomeAssistant, config_entry: ConfigEntry
35+
) -> None:
36+
"""Service should raise when entry exists but is not loaded."""
37+
38+
# Add a config entry but do not set it up (state is not LOADED)
39+
assert config_entry.state is ConfigEntryState.NOT_LOADED
40+
# Ensure the integration is set up so the service is registered
41+
assert await async_setup(hass, {})
42+
43+
with pytest.raises(ServiceValidationError, match="Entry not loaded"):
44+
await hass.services.async_call("vesync", SERVICE_UPDATE_DEVS, {}, blocking=True)
45+
46+
47+
async def test_async_new_device_discovery(
48+
hass: HomeAssistant,
49+
config_entry: ConfigEntry,
50+
manager: VeSync,
51+
fan,
52+
entity_registry: er.EntityRegistry,
53+
) -> None:
54+
"""Test new device discovery."""
55+
56+
# Entry should not be set up yet; we'll install a fan before setup
57+
assert config_entry.state is ConfigEntryState.NOT_LOADED
58+
59+
# Set up the config entry (no devices initially)
60+
assert await hass.config_entries.async_setup(config_entry.entry_id)
61+
await hass.async_block_till_done()
62+
63+
assert config_entry.state is ConfigEntryState.LOADED
64+
assert not hass.data[DOMAIN][VS_MANAGER].devices
65+
66+
# Simulate the manager discovering a new fan when get_devices is called
67+
manager.get_devices = AsyncMock(
68+
side_effect=lambda: manager._dev_list["fans"].append(fan)
69+
)
70+
71+
# Call the service that should trigger discovery and platform setup
72+
await hass.services.async_call(DOMAIN, SERVICE_UPDATE_DEVS, {}, blocking=True)
73+
await hass.async_block_till_done()
74+
75+
assert manager.get_devices.call_count == 1
76+
77+
# Verify an entity for the new fan was created in Home Assistant
78+
fan_entry = next(
79+
(
80+
e
81+
for e in entity_registry.entities.values()
82+
if e.unique_id == fan.cid and e.domain == "fan"
83+
),
84+
None,
85+
)
86+
assert fan_entry is not None

0 commit comments

Comments
 (0)