From d18630020fa1233a9b93d384159c8a6931d21613 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 3 Feb 2026 11:15:05 +0100 Subject: [PATCH] Move songpal service registration (#162131) --- homeassistant/components/songpal/__init__.py | 3 ++ homeassistant/components/songpal/const.py | 1 - .../components/songpal/media_player.py | 16 ++-------- homeassistant/components/songpal/services.py | 32 +++++++++++++++++++ tests/components/songpal/test_media_player.py | 6 ++-- 5 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 homeassistant/components/songpal/services.py diff --git a/homeassistant/components/songpal/__init__.py b/homeassistant/components/songpal/__init__.py index aa3f850c9e3..18f9aae8f13 100644 --- a/homeassistant/components/songpal/__init__.py +++ b/homeassistant/components/songpal/__init__.py @@ -9,6 +9,7 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType from .const import CONF_ENDPOINT, DOMAIN +from .services import async_setup_services SONGPAL_CONFIG_SCHEMA = vol.Schema( {vol.Optional(CONF_NAME): cv.string, vol.Required(CONF_ENDPOINT): cv.string} @@ -24,6 +25,8 @@ PLATFORMS = [Platform.MEDIA_PLAYER] async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up songpal environment.""" + async_setup_services(hass) + if (conf := config.get(DOMAIN)) is None: return True for config_entry in conf: diff --git a/homeassistant/components/songpal/const.py b/homeassistant/components/songpal/const.py index 04b005e2a09..d512c08b2e8 100644 --- a/homeassistant/components/songpal/const.py +++ b/homeassistant/components/songpal/const.py @@ -1,7 +1,6 @@ """Constants for the Songpal component.""" DOMAIN = "songpal" -SET_SOUND_SETTING = "set_sound_setting" CONF_ENDPOINT = "endpoint" diff --git a/homeassistant/components/songpal/media_player.py b/homeassistant/components/songpal/media_player.py index 3fc75d712a7..413700edef7 100644 --- a/homeassistant/components/songpal/media_player.py +++ b/homeassistant/components/songpal/media_player.py @@ -16,7 +16,6 @@ from songpal import ( VolumeChange, ) from songpal.containers import Setting -import voluptuous as vol from homeassistant.components.media_player import ( MediaPlayerDeviceClass, @@ -28,11 +27,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME, EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant from homeassistant.exceptions import PlatformNotReady -from homeassistant.helpers import ( - config_validation as cv, - device_registry as dr, - entity_platform, -) +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import ( AddConfigEntryEntitiesCallback, @@ -40,7 +35,7 @@ from homeassistant.helpers.entity_platform import ( ) from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from .const import CONF_ENDPOINT, DOMAIN, ERROR_REQUEST_RETRY, SET_SOUND_SETTING +from .const import CONF_ENDPOINT, DOMAIN, ERROR_REQUEST_RETRY _LOGGER = logging.getLogger(__name__) @@ -86,13 +81,6 @@ async def async_setup_entry( songpal_entity = SongpalEntity(name, device) async_add_entities([songpal_entity], True) - platform = entity_platform.async_get_current_platform() - platform.async_register_entity_service( - SET_SOUND_SETTING, - {vol.Required(PARAM_NAME): cv.string, vol.Required(PARAM_VALUE): cv.string}, - "async_set_sound_setting", - ) - class SongpalEntity(MediaPlayerEntity): """Class representing a Songpal device.""" diff --git a/homeassistant/components/songpal/services.py b/homeassistant/components/songpal/services.py new file mode 100644 index 00000000000..f5756799901 --- /dev/null +++ b/homeassistant/components/songpal/services.py @@ -0,0 +1,32 @@ +"""Support for Songpal-enabled (Sony) media devices.""" + +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 + +PARAM_NAME = "name" +PARAM_VALUE = "value" + +SET_SOUND_SETTING = "set_sound_setting" + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up services.""" + service.async_register_platform_entity_service( + hass, + DOMAIN, + SET_SOUND_SETTING, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema={ + vol.Required(PARAM_NAME): cv.string, + vol.Required(PARAM_VALUE): cv.string, + }, + func="async_set_sound_setting", + ) diff --git a/tests/components/songpal/test_media_player.py b/tests/components/songpal/test_media_player.py index 2baea6cb5c9..4b82bd35987 100644 --- a/tests/components/songpal/test_media_player.py +++ b/tests/components/songpal/test_media_player.py @@ -17,10 +17,8 @@ from songpal.notification import SettingChange from homeassistant.components import media_player, songpal from homeassistant.components.media_player import MediaPlayerEntityFeature -from homeassistant.components.songpal.const import ( - ERROR_REQUEST_RETRY, - SET_SOUND_SETTING, -) +from homeassistant.components.songpal.const import ERROR_REQUEST_RETRY +from homeassistant.components.songpal.services import SET_SOUND_SETTING from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er