From a715ec318c54a8f6977b3ff2695828b773f925c8 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 3 Feb 2026 11:14:39 +0100 Subject: [PATCH] Move snapcast service registration (#162130) --- homeassistant/components/snapcast/__init__.py | 11 +++++ homeassistant/components/snapcast/const.py | 6 --- .../components/snapcast/media_player.py | 32 +------------ homeassistant/components/snapcast/services.py | 46 +++++++++++++++++++ 4 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 homeassistant/components/snapcast/services.py diff --git a/homeassistant/components/snapcast/__init__.py b/homeassistant/components/snapcast/__init__.py index 9c1602494e5..0888f339a7d 100644 --- a/homeassistant/components/snapcast/__init__.py +++ b/homeassistant/components/snapcast/__init__.py @@ -4,9 +4,20 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, PLATFORMS from .coordinator import SnapcastUpdateCoordinator +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 the component.""" + async_setup_services(hass) + return True async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/snapcast/const.py b/homeassistant/components/snapcast/const.py index 4ff86745975..34c4c3a993b 100644 --- a/homeassistant/components/snapcast/const.py +++ b/homeassistant/components/snapcast/const.py @@ -7,11 +7,5 @@ PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER] CLIENT_PREFIX = "snapcast_client_" CLIENT_SUFFIX = "Snapcast Client" -SERVICE_SNAPSHOT = "snapshot" -SERVICE_RESTORE = "restore" -SERVICE_SET_LATENCY = "set_latency" - -ATTR_LATENCY = "latency" - DOMAIN = "snapcast" DEFAULT_TITLE = "Snapcast" diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 5f31ce1d7b8..d4d5f98211e 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -8,7 +8,6 @@ from typing import Any from snapcast.control.client import Snapclient from snapcast.control.group import Snapgroup -import voluptuous as vol from homeassistant.components.media_player import ( DOMAIN as MEDIA_PLAYER_DOMAIN, @@ -21,22 +20,10 @@ from homeassistant.components.media_player import ( from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ServiceValidationError -from homeassistant.helpers import ( - config_validation as cv, - entity_platform, - entity_registry as er, -) +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import ( - ATTR_LATENCY, - CLIENT_PREFIX, - CLIENT_SUFFIX, - DOMAIN, - SERVICE_RESTORE, - SERVICE_SET_LATENCY, - SERVICE_SNAPSHOT, -) +from .const import CLIENT_PREFIX, CLIENT_SUFFIX, DOMAIN from .coordinator import SnapcastUpdateCoordinator from .entity import SnapcastCoordinatorEntity @@ -49,19 +36,6 @@ STREAM_STATUS = { _LOGGER = logging.getLogger(__name__) -def register_services() -> None: - """Register snapcast services.""" - platform = entity_platform.async_get_current_platform() - - platform.async_register_entity_service(SERVICE_SNAPSHOT, None, "async_snapshot") - platform.async_register_entity_service(SERVICE_RESTORE, None, "async_restore") - platform.async_register_entity_service( - SERVICE_SET_LATENCY, - {vol.Required(ATTR_LATENCY): cv.positive_int}, - "async_set_latency", - ) - - async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, @@ -72,8 +46,6 @@ async def async_setup_entry( # Fetch coordinator from global data coordinator: SnapcastUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] - register_services() - _known_client_ids: set[str] = set() @callback diff --git a/homeassistant/components/snapcast/services.py b/homeassistant/components/snapcast/services.py new file mode 100644 index 00000000000..6e2e1d60a21 --- /dev/null +++ b/homeassistant/components/snapcast/services.py @@ -0,0 +1,46 @@ +"""Support for interacting with Snapcast clients.""" + +from __future__ import annotations + +import voluptuous as vol + +from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv, service + +from .const import DOMAIN + +SERVICE_SNAPSHOT = "snapshot" +SERVICE_RESTORE = "restore" +SERVICE_SET_LATENCY = "set_latency" + +ATTR_LATENCY = "latency" + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up services.""" + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SNAPSHOT, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema=None, + func="async_snapshot", + ) + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_RESTORE, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema=None, + func="async_restore", + ) + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_LATENCY, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema={vol.Required(ATTR_LATENCY): cv.positive_int}, + func="async_set_latency", + )