diff --git a/homeassistant/components/vesync/__init__.py b/homeassistant/components/vesync/__init__.py index ece7a2d7ad0..133713b3352 100644 --- a/homeassistant/components/vesync/__init__.py +++ b/homeassistant/components/vesync/__init__.py @@ -7,15 +7,18 @@ from pyvesync.utils.errors import VeSyncLoginError from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import config_validation as cv, entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import DeviceEntry -from homeassistant.helpers.dispatcher import async_dispatcher_send +from homeassistant.helpers.typing import ConfigType -from .const import DOMAIN, SERVICE_UPDATE_DEVS, VS_COORDINATOR, VS_MANAGER +from .const import DOMAIN, VS_COORDINATOR, VS_MANAGER from .coordinator import VeSyncDataCoordinator +from .services import async_setup_services + +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) PLATFORMS = [ Platform.BINARY_SENSOR, @@ -32,6 +35,14 @@ PLATFORMS = [ _LOGGER = logging.getLogger(__name__) +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up my integration.""" + + async_setup_services(hass) + + return True + + async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Set up Vesync as config entry.""" username = config_entry.data[CONF_USERNAME] @@ -62,22 +73,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) - async def async_new_device_discovery(service: ServiceCall) -> None: - """Discover and add new devices.""" - manager = hass.data[DOMAIN][VS_MANAGER] - known_devices = list(manager.devices) - await manager.get_devices() - new_devices = [ - device for device in manager.devices if device not in known_devices - ] - - if new_devices: - async_dispatcher_send(hass, "vesync_new_devices", new_devices) - - hass.services.async_register( - DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery - ) - return True diff --git a/homeassistant/components/vesync/services.py b/homeassistant/components/vesync/services.py new file mode 100644 index 00000000000..c1a9bc5e638 --- /dev/null +++ b/homeassistant/components/vesync/services.py @@ -0,0 +1,36 @@ +"""Support for VeSync Services.""" + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers.dispatcher import async_dispatcher_send + +from .const import DOMAIN, SERVICE_UPDATE_DEVS, VS_DEVICES, VS_DISCOVERY, VS_MANAGER + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Handle for services.""" + + hass.services.async_register( + DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery + ) + + +async def async_new_device_discovery(call: ServiceCall) -> None: + """Discover and add new devices.""" + + entries = call.hass.config_entries.async_entries(DOMAIN) + entry = entries[0] if entries else None + + if not entry: + raise ServiceValidationError("Entry not found") + if entry.state is not ConfigEntryState.LOADED: + raise ServiceValidationError("Entry not loaded") + manager = call.hass.data[DOMAIN][VS_MANAGER] + known_devices = list(manager.devices) + await manager.get_devices() + new_devices = [device for device in manager.devices if device not in known_devices] + + if new_devices: + async_dispatcher_send(call.hass, VS_DISCOVERY.format(VS_DEVICES), new_devices) diff --git a/tests/components/vesync/test_init.py b/tests/components/vesync/test_init.py index 758ae61858a..97d27fe221c 100644 --- a/tests/components/vesync/test_init.py +++ b/tests/components/vesync/test_init.py @@ -6,7 +6,6 @@ from pyvesync import VeSync from pyvesync.utils.errors import VeSyncLoginError from homeassistant.components.vesync import ( - SERVICE_UPDATE_DEVS, async_remove_config_entry_device, async_setup_entry, ) @@ -91,34 +90,6 @@ async def test_async_setup_entry__loads_fans( assert list(hass.data[DOMAIN][VS_MANAGER].devices) == [fan] -async def test_async_new_device_discovery( - hass: HomeAssistant, config_entry: ConfigEntry, manager: VeSync, fan, humidifier -) -> None: - """Test new device discovery.""" - - assert await hass.config_entries.async_setup(config_entry.entry_id) - # Assert platforms loaded - await hass.async_block_till_done() - assert config_entry.state is ConfigEntryState.LOADED - assert not hass.data[DOMAIN][VS_MANAGER].devices - - # Mock discovery of new fan which would get added to VS_DEVICES. - manager._dev_list["fans"].append(fan) - await hass.services.async_call(DOMAIN, SERVICE_UPDATE_DEVS, {}, blocking=True) - - assert manager.get_devices.call_count == 1 - assert hass.data[DOMAIN][VS_MANAGER] == manager - assert list(hass.data[DOMAIN][VS_MANAGER].devices) == [fan] - - # Mock discovery of new humidifier which would invoke discovery in all platforms. - manager._dev_list["humidifiers"].append(humidifier) - await hass.services.async_call(DOMAIN, SERVICE_UPDATE_DEVS, {}, blocking=True) - - assert manager.get_devices.call_count == 2 - assert hass.data[DOMAIN][VS_MANAGER] == manager - assert list(hass.data[DOMAIN][VS_MANAGER].devices) == [fan, humidifier] - - async def test_migrate_config_entry( hass: HomeAssistant, switch_old_id_config_entry: MockConfigEntry, diff --git a/tests/components/vesync/test_services.py b/tests/components/vesync/test_services.py new file mode 100644 index 00000000000..6508a49f311 --- /dev/null +++ b/tests/components/vesync/test_services.py @@ -0,0 +1,86 @@ +"""Tests for VeSync services.""" + +from unittest.mock import AsyncMock + +import pytest +from pyvesync import VeSync + +from homeassistant.components.vesync import async_setup +from homeassistant.components.vesync.const import ( + DOMAIN, + SERVICE_UPDATE_DEVS, + VS_MANAGER, +) +from homeassistant.config_entries import ConfigEntry, ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers import entity_registry as er + + +async def test_async_new_device_discovery_no_entry( + hass: HomeAssistant, +) -> None: + """Service should raise when no config entry exists.""" + + # Ensure the integration is set up so the service is registered + assert await async_setup(hass, {}) + + # No entries for the domain, service should raise + with pytest.raises(ServiceValidationError, match="Entry not found"): + await hass.services.async_call("vesync", SERVICE_UPDATE_DEVS, {}, blocking=True) + + +async def test_async_new_device_discovery_entry_not_loaded( + hass: HomeAssistant, config_entry: ConfigEntry +) -> None: + """Service should raise when entry exists but is not loaded.""" + + # Add a config entry but do not set it up (state is not LOADED) + assert config_entry.state is ConfigEntryState.NOT_LOADED + # Ensure the integration is set up so the service is registered + assert await async_setup(hass, {}) + + with pytest.raises(ServiceValidationError, match="Entry not loaded"): + await hass.services.async_call("vesync", SERVICE_UPDATE_DEVS, {}, blocking=True) + + +async def test_async_new_device_discovery( + hass: HomeAssistant, + config_entry: ConfigEntry, + manager: VeSync, + fan, + entity_registry: er.EntityRegistry, +) -> None: + """Test new device discovery.""" + + # Entry should not be set up yet; we'll install a fan before setup + assert config_entry.state is ConfigEntryState.NOT_LOADED + + # Set up the config entry (no devices initially) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + assert not hass.data[DOMAIN][VS_MANAGER].devices + + # Simulate the manager discovering a new fan when get_devices is called + manager.get_devices = AsyncMock( + side_effect=lambda: manager._dev_list["fans"].append(fan) + ) + + # Call the service that should trigger discovery and platform setup + await hass.services.async_call(DOMAIN, SERVICE_UPDATE_DEVS, {}, blocking=True) + await hass.async_block_till_done() + + assert manager.get_devices.call_count == 1 + + # Verify an entity for the new fan was created in Home Assistant + fan_entry = next( + ( + e + for e in entity_registry.entities.values() + if e.unique_id == fan.cid and e.domain == "fan" + ), + None, + ) + assert fan_entry is not None