core/homeassistant/components/soundtouch/__init__.py

143 lines
4.3 KiB
Python
Raw Normal View History

Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
"""The soundtouch component."""
import logging
from libsoundtouch import soundtouch_device
from libsoundtouch.device import SoundTouchDevice
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
from .const import (
DOMAIN,
SERVICE_ADD_ZONE_SLAVE,
SERVICE_CREATE_ZONE,
SERVICE_PLAY_EVERYWHERE,
SERVICE_REMOVE_ZONE_SLAVE,
)
_LOGGER = logging.getLogger(__name__)
SERVICE_PLAY_EVERYWHERE_SCHEMA = vol.Schema({vol.Required("master"): cv.entity_id})
SERVICE_CREATE_ZONE_SCHEMA = vol.Schema(
{
vol.Required("master"): cv.entity_id,
vol.Required("slaves"): cv.entity_ids,
}
)
SERVICE_ADD_ZONE_SCHEMA = vol.Schema(
{
vol.Required("master"): cv.entity_id,
vol.Required("slaves"): cv.entity_ids,
}
)
SERVICE_REMOVE_ZONE_SCHEMA = vol.Schema(
{
vol.Required("master"): cv.entity_id,
vol.Required("slaves"): cv.entity_ids,
}
)
PLATFORMS = [Platform.MEDIA_PLAYER]
class SoundTouchData:
"""SoundTouch data stored in the Home Assistant data object."""
def __init__(self, device: SoundTouchDevice) -> None:
"""Initialize the SoundTouch data object for a device."""
self.device = device
self.media_player = None
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up Bose SoundTouch component."""
async def service_handle(service: ServiceCall) -> None:
"""Handle the applying of a service."""
master_id = service.data.get("master")
slaves_ids = service.data.get("slaves")
slaves = []
if slaves_ids:
slaves = [
data.media_player
for data in hass.data[DOMAIN].values()
if data.media_player.entity_id in slaves_ids
]
master = next(
iter(
[
data.media_player
for data in hass.data[DOMAIN].values()
if data.media_player.entity_id == master_id
]
),
None,
)
if master is None:
_LOGGER.warning("Unable to find master with entity_id: %s", str(master_id))
return
if service.service == SERVICE_PLAY_EVERYWHERE:
slaves = [
data.media_player
for data in hass.data[DOMAIN].values()
if data.media_player.entity_id != master_id
]
await hass.async_add_executor_job(master.create_zone, slaves)
elif service.service == SERVICE_CREATE_ZONE:
await hass.async_add_executor_job(master.create_zone, slaves)
elif service.service == SERVICE_REMOVE_ZONE_SLAVE:
await hass.async_add_executor_job(master.remove_zone_slave, slaves)
elif service.service == SERVICE_ADD_ZONE_SLAVE:
await hass.async_add_executor_job(master.add_zone_slave, slaves)
hass.services.async_register(
DOMAIN,
SERVICE_PLAY_EVERYWHERE,
service_handle,
schema=SERVICE_PLAY_EVERYWHERE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_CREATE_ZONE,
service_handle,
schema=SERVICE_CREATE_ZONE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_REMOVE_ZONE_SLAVE,
service_handle,
schema=SERVICE_REMOVE_ZONE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_ADD_ZONE_SLAVE,
service_handle,
schema=SERVICE_ADD_ZONE_SCHEMA,
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Bose SoundTouch from a config entry."""
device = await hass.async_add_executor_job(soundtouch_device, entry.data[CONF_HOST])
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = SoundTouchData(device)
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
del hass.data[DOMAIN][entry.entry_id]
return unload_ok