Skip to content
56 changes: 12 additions & 44 deletions homeassistant/components/songpal/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
SUPPORT_VOLUME_STEP,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_NAME,
EVENT_HOMEASSISTANT_STOP,
STATE_OFF,
STATE_ON,
)
from homeassistant.const import CONF_NAME, EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
entity_platform,
)
from homeassistant.helpers.typing import HomeAssistantType

from .const import CONF_ENDPOINT, DOMAIN, SET_SOUND_SETTING
Expand All @@ -50,14 +48,6 @@
| SUPPORT_TURN_OFF
)

SET_SOUND_SCHEMA = vol.Schema(
{
vol.Optional(ATTR_ENTITY_ID): cv.entity_id,
vol.Required(PARAM_NAME): cv.string,
vol.Required(PARAM_VALUE): cv.string,
}
)


async def async_setup_platform(
hass: HomeAssistantType, config: dict, async_add_entities, discovery_info=None
Expand All @@ -72,46 +62,23 @@ async def async_setup_entry(
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
) -> None:
"""Set up songpal media player."""
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {}

name = config_entry.data[CONF_NAME]
endpoint = config_entry.data[CONF_ENDPOINT]

if endpoint in hass.data[DOMAIN]:
_LOGGER.debug("The endpoint exists already, skipping setup.")
return

device = SongpalDevice(name, endpoint)
try:
await device.initialize()
Comment thread
shenxn marked this conversation as resolved.
Outdated
except SongpalException as ex:
_LOGGER.error("Unable to get methods from songpal: %s", ex)
raise PlatformNotReady

hass.data[DOMAIN][endpoint] = device

async_add_entities([device], True)

async def async_service_handler(service):
"""Service handler."""
entity_id = service.data.get("entity_id", None)
params = {
key: value for key, value in service.data.items() if key != ATTR_ENTITY_ID
}

for device in hass.data[DOMAIN].values():
if device.entity_id == entity_id or entity_id is None:
_LOGGER.debug(
"Calling %s (entity: %s) with params %s", service, entity_id, params
)

await device.async_set_sound_setting(
params[PARAM_NAME], params[PARAM_VALUE]
)

hass.services.async_register(
DOMAIN, SET_SOUND_SETTING, async_service_handler, schema=SET_SOUND_SCHEMA
platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
SET_SOUND_SETTING,
{vol.Required(PARAM_NAME): cv.string, vol.Required(PARAM_VALUE): cv.string},
"async_set_sound_setting",
)


Expand Down Expand Up @@ -246,6 +213,7 @@ def available(self):

async def async_set_sound_setting(self, name, value):
"""Change a setting on the device."""
_LOGGER.debug("Calling set_sound_setting with %s: %s", name, value)
await self.dev.set_sound_settings(name, value)

async def async_update(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/components/songpal/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test the songpal config flow."""
import copy

from asynctest import MagicMock, patch
from songpal import SongpalException
from songpal.containers import InterfaceInfo

Expand All @@ -15,6 +14,7 @@
RESULT_TYPE_FORM,
)

from tests.async_mock import MagicMock, patch
from tests.common import MockConfigEntry

UDN = "uuid:1234"
Expand Down