mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Rework deCONZ services to load once and never unload (#117592)
* Rework deCONZ services to load once and never unload * Fix hassfest
This commit is contained in:
parent
4300ff6b60
commit
657b3ceedc
@ -6,13 +6,23 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .config_flow import get_master_hub
|
||||
from .const import CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
|
||||
from .deconz_event import async_setup_events, async_unload_events
|
||||
from .errors import AuthenticationRequired, CannotConnect
|
||||
from .hub import DeconzHub, get_deconz_api
|
||||
from .services import async_setup_services, async_unload_services
|
||||
from .services import async_setup_services
|
||||
|
||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up services."""
|
||||
async_setup_services(hass)
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
@ -33,9 +43,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
||||
except AuthenticationRequired as err:
|
||||
raise ConfigEntryAuthFailed from err
|
||||
|
||||
if not hass.data[DOMAIN]:
|
||||
async_setup_services(hass)
|
||||
|
||||
hub = hass.data[DOMAIN][config_entry.entry_id] = DeconzHub(hass, config_entry, api)
|
||||
await hub.async_update_device_registry()
|
||||
|
||||
@ -58,10 +65,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
||||
hub: DeconzHub = hass.data[DOMAIN].pop(config_entry.entry_id)
|
||||
async_unload_events(hub)
|
||||
|
||||
if not hass.data[DOMAIN]:
|
||||
async_unload_services(hass)
|
||||
|
||||
elif hub.master:
|
||||
if hass.data[DOMAIN] and hub.master:
|
||||
await async_update_master_hub(hass, config_entry)
|
||||
new_master_hub = next(iter(hass.data[DOMAIN].values()))
|
||||
await async_update_master_hub(hass, new_master_hub.config_entry)
|
||||
|
@ -103,13 +103,6 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def async_unload_services(hass: HomeAssistant) -> None:
|
||||
"""Unload deCONZ services."""
|
||||
for service in SUPPORTED_SERVICES:
|
||||
hass.services.async_remove(DOMAIN, service)
|
||||
|
||||
|
||||
async def async_configure_service(hub: DeconzHub, data: ReadOnlyDict) -> None:
|
||||
"""Set attribute of device in deCONZ.
|
||||
|
||||
|
@ -18,7 +18,6 @@ from homeassistant.components.deconz.services import (
|
||||
SERVICE_ENTITY,
|
||||
SERVICE_FIELD,
|
||||
SERVICE_REMOVE_ORPHANED_ENTRIES,
|
||||
SUPPORTED_SERVICES,
|
||||
)
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
@ -37,40 +36,6 @@ from tests.common import async_capture_events
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def test_service_setup_and_unload(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Verify service setup works."""
|
||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||
for service in SUPPORTED_SERVICES:
|
||||
assert hass.services.has_service(DECONZ_DOMAIN, service)
|
||||
|
||||
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
for service in SUPPORTED_SERVICES:
|
||||
assert not hass.services.has_service(DECONZ_DOMAIN, service)
|
||||
|
||||
|
||||
@patch("homeassistant.core.ServiceRegistry.async_remove")
|
||||
@patch("homeassistant.core.ServiceRegistry.async_register")
|
||||
async def test_service_setup_and_unload_not_called_if_multiple_integrations_detected(
|
||||
register_service_mock,
|
||||
remove_service_mock,
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
) -> None:
|
||||
"""Make sure that services are only setup and removed once."""
|
||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||
register_service_mock.reset_mock()
|
||||
config_entry_2 = await setup_deconz_integration(hass, aioclient_mock, entry_id=2)
|
||||
register_service_mock.assert_not_called()
|
||||
|
||||
register_service_mock.assert_not_called()
|
||||
assert await hass.config_entries.async_unload(config_entry_2.entry_id)
|
||||
remove_service_mock.assert_not_called()
|
||||
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
assert remove_service_mock.call_count == 3
|
||||
|
||||
|
||||
async def test_configure_service_with_field(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user