diff --git a/homeassistant/components/linkplay/__init__.py b/homeassistant/components/linkplay/__init__.py index 2da73666cc4..98481feb9ff 100644 --- a/homeassistant/components/linkplay/__init__.py +++ b/homeassistant/components/linkplay/__init__.py @@ -12,10 +12,15 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST 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, SHARED_DATA, LinkPlaySharedData +from .services import async_setup_services from .utils import async_get_client_session +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + @dataclass class LinkPlayData: @@ -27,6 +32,12 @@ class LinkPlayData: type LinkPlayConfigEntry = ConfigEntry[LinkPlayData] +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: LinkPlayConfigEntry) -> bool: """Async setup hass config entry. Called when an entry has been setup.""" diff --git a/homeassistant/components/linkplay/media_player.py b/homeassistant/components/linkplay/media_player.py index ee1cdfe67e8..702aa0c7629 100644 --- a/homeassistant/components/linkplay/media_player.py +++ b/homeassistant/components/linkplay/media_player.py @@ -10,7 +10,6 @@ from linkplay.bridge import LinkPlayBridge from linkplay.consts import EqualizerMode, LoopMode, PlayingMode, PlayingStatus from linkplay.controller import LinkPlayController, LinkPlayMultiroom from linkplay.exceptions import LinkPlayRequestException -import voluptuous as vol from homeassistant.components import media_source from homeassistant.components.media_player import ( @@ -25,7 +24,6 @@ from homeassistant.components.media_player import ( ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError -from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util.dt import utcnow @@ -106,15 +104,6 @@ SEEKABLE_FEATURES: MediaPlayerEntityFeature = ( | MediaPlayerEntityFeature.SEEK ) -SERVICE_PLAY_PRESET = "play_preset" -ATTR_PRESET_NUMBER = "preset_number" - -SERVICE_PLAY_PRESET_SCHEMA = cv.make_entity_service_schema( - { - vol.Required(ATTR_PRESET_NUMBER): cv.positive_int, - } -) - RETRY_POLL_MAXIMUM = 3 SCAN_INTERVAL = timedelta(seconds=5) PARALLEL_UPDATES = 1 @@ -126,14 +115,6 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up a media player from a config entry.""" - - # register services - platform = entity_platform.async_get_current_platform() - platform.async_register_entity_service( - SERVICE_PLAY_PRESET, SERVICE_PLAY_PRESET_SCHEMA, "async_play_preset" - ) - - # add entities async_add_entities([LinkPlayMediaPlayerEntity(entry.runtime_data.bridge)]) diff --git a/homeassistant/components/linkplay/services.py b/homeassistant/components/linkplay/services.py new file mode 100644 index 00000000000..bccb31148e4 --- /dev/null +++ b/homeassistant/components/linkplay/services.py @@ -0,0 +1,33 @@ +"""Support for LinkPlay media players.""" + +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_PLAY_PRESET = "play_preset" +ATTR_PRESET_NUMBER = "preset_number" + +SERVICE_PLAY_PRESET_SCHEMA = cv.make_entity_service_schema( + { + vol.Required(ATTR_PRESET_NUMBER): cv.positive_int, + } +) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up services.""" + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_PLAY_PRESET, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema=SERVICE_PLAY_PRESET_SCHEMA, + func="async_play_preset", + )