diff --git a/homeassistant/components/monoprice/__init__.py b/homeassistant/components/monoprice/__init__.py index 1f5df2ca194c..3cd864f41493 100644 --- a/homeassistant/components/monoprice/__init__.py +++ b/homeassistant/components/monoprice/__init__.py @@ -12,13 +12,18 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PORT, Platform 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 CONF_NOT_FIRST_RUN +from .const import CONF_NOT_FIRST_RUN, DOMAIN +from .services import async_setup_services PLATFORMS = [Platform.MEDIA_PLAYER] _LOGGER = logging.getLogger(__name__) +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + type MonopriceConfigEntry = ConfigEntry[MonopriceRuntimeData] @@ -30,6 +35,12 @@ class MonopriceRuntimeData: first_run: bool +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: MonopriceConfigEntry) -> bool: """Set up Monoprice 6-Zone Amplifier from a config entry.""" port = entry.data[CONF_PORT] diff --git a/homeassistant/components/monoprice/media_player.py b/homeassistant/components/monoprice/media_player.py index 4561f29ba566..fe3e158e163c 100644 --- a/homeassistant/components/monoprice/media_player.py +++ b/homeassistant/components/monoprice/media_player.py @@ -13,12 +13,11 @@ from homeassistant.components.media_player import ( ) from homeassistant.const import CONF_PORT from homeassistant.core import HomeAssistant -from homeassistant.helpers import config_validation as cv, entity_platform, service from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from . import MonopriceConfigEntry -from .const import CONF_SOURCES, DOMAIN, SERVICE_RESTORE, SERVICE_SNAPSHOT +from .const import CONF_SOURCES, DOMAIN _LOGGER = logging.getLogger(__name__) @@ -72,39 +71,6 @@ async def async_setup_entry( # only call update before add if it's the first run so we can try to detect zones async_add_entities(entities, config_entry.runtime_data.first_run) - platform = entity_platform.async_get_current_platform() - - def _call_service(entities, service_call): - for entity in entities: - if service_call.service == SERVICE_SNAPSHOT: - entity.snapshot() - elif service_call.service == SERVICE_RESTORE: - entity.restore() - - @service.verify_domain_control(DOMAIN) - async def async_service_handle(service_call: core.ServiceCall) -> None: - """Handle for services.""" - entities = await platform.async_extract_from_service(service_call) - - if not entities: - return - - hass.async_add_executor_job(_call_service, entities, service_call) - - hass.services.async_register( - DOMAIN, - SERVICE_SNAPSHOT, - async_service_handle, - schema=cv.make_entity_service_schema({}), - ) - - hass.services.async_register( - DOMAIN, - SERVICE_RESTORE, - async_service_handle, - schema=cv.make_entity_service_schema({}), - ) - class MonopriceZone(MediaPlayerEntity): """Representation of a Monoprice amplifier zone.""" @@ -180,7 +146,6 @@ class MonopriceZone(MediaPlayerEntity): """Restore saved state.""" if self._snapshot: self._monoprice.restore_zone(self._snapshot) - self.schedule_update_ha_state(True) def select_source(self, source: str) -> None: """Set input source.""" diff --git a/homeassistant/components/monoprice/services.py b/homeassistant/components/monoprice/services.py new file mode 100644 index 000000000000..4211c1e759c5 --- /dev/null +++ b/homeassistant/components/monoprice/services.py @@ -0,0 +1,30 @@ +"""Services for the monoprice integration.""" + +from __future__ import annotations + +from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import service + +from .const import DOMAIN, SERVICE_RESTORE, SERVICE_SNAPSHOT + + +@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="snapshot", + ) + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_RESTORE, + entity_domain=MEDIA_PLAYER_DOMAIN, + schema=None, + func="restore", + )