Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The soundtouch component."""
2 
3 import logging
4 
5 from libsoundtouch import soundtouch_device
6 from libsoundtouch.device import SoundTouchDevice
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_HOST, Platform
11 from homeassistant.core import HomeAssistant, ServiceCall
13 from homeassistant.helpers.typing import ConfigType
14 
15 from .const import (
16  DOMAIN,
17  SERVICE_ADD_ZONE_SLAVE,
18  SERVICE_CREATE_ZONE,
19  SERVICE_PLAY_EVERYWHERE,
20  SERVICE_REMOVE_ZONE_SLAVE,
21 )
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 SERVICE_PLAY_EVERYWHERE_SCHEMA = vol.Schema({vol.Required("master"): cv.entity_id})
26 SERVICE_CREATE_ZONE_SCHEMA = vol.Schema(
27  {
28  vol.Required("master"): cv.entity_id,
29  vol.Required("slaves"): cv.entity_ids,
30  }
31 )
32 SERVICE_ADD_ZONE_SCHEMA = vol.Schema(
33  {
34  vol.Required("master"): cv.entity_id,
35  vol.Required("slaves"): cv.entity_ids,
36  }
37 )
38 SERVICE_REMOVE_ZONE_SCHEMA = vol.Schema(
39  {
40  vol.Required("master"): cv.entity_id,
41  vol.Required("slaves"): cv.entity_ids,
42  }
43 )
44 
45 PLATFORMS = [Platform.MEDIA_PLAYER]
46 
47 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
48 
49 
51  """SoundTouch data stored in the Home Assistant data object."""
52 
53  def __init__(self, device: SoundTouchDevice) -> None:
54  """Initialize the SoundTouch data object for a device."""
55  self.devicedevice = device
56  self.media_playermedia_player = None
57 
58 
59 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
60  """Set up Bose SoundTouch component."""
61 
62  async def service_handle(service: ServiceCall) -> None:
63  """Handle the applying of a service."""
64  master_id = service.data.get("master")
65  slaves_ids = service.data.get("slaves")
66  slaves = []
67  if slaves_ids:
68  slaves = [
69  data.media_player
70  for data in hass.data[DOMAIN].values()
71  if data.media_player.entity_id in slaves_ids
72  ]
73 
74  master = next(
75  iter(
76  [
77  data.media_player
78  for data in hass.data[DOMAIN].values()
79  if data.media_player.entity_id == master_id
80  ]
81  ),
82  None,
83  )
84 
85  if master is None:
86  _LOGGER.warning("Unable to find master with entity_id: %s", str(master_id))
87  return
88 
89  if service.service == SERVICE_PLAY_EVERYWHERE:
90  slaves = [
91  data.media_player
92  for data in hass.data[DOMAIN].values()
93  if data.media_player.entity_id != master_id
94  ]
95  await hass.async_add_executor_job(master.create_zone, slaves)
96  elif service.service == SERVICE_CREATE_ZONE:
97  await hass.async_add_executor_job(master.create_zone, slaves)
98  elif service.service == SERVICE_REMOVE_ZONE_SLAVE:
99  await hass.async_add_executor_job(master.remove_zone_slave, slaves)
100  elif service.service == SERVICE_ADD_ZONE_SLAVE:
101  await hass.async_add_executor_job(master.add_zone_slave, slaves)
102 
103  hass.services.async_register(
104  DOMAIN,
105  SERVICE_PLAY_EVERYWHERE,
106  service_handle,
107  schema=SERVICE_PLAY_EVERYWHERE_SCHEMA,
108  )
109  hass.services.async_register(
110  DOMAIN,
111  SERVICE_CREATE_ZONE,
112  service_handle,
113  schema=SERVICE_CREATE_ZONE_SCHEMA,
114  )
115  hass.services.async_register(
116  DOMAIN,
117  SERVICE_REMOVE_ZONE_SLAVE,
118  service_handle,
119  schema=SERVICE_REMOVE_ZONE_SCHEMA,
120  )
121  hass.services.async_register(
122  DOMAIN,
123  SERVICE_ADD_ZONE_SLAVE,
124  service_handle,
125  schema=SERVICE_ADD_ZONE_SCHEMA,
126  )
127 
128  return True
129 
130 
131 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
132  """Set up Bose SoundTouch from a config entry."""
133  device = await hass.async_add_executor_job(soundtouch_device, entry.data[CONF_HOST])
134 
135  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = SoundTouchData(device)
136 
137  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
138  return True
139 
140 
141 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
142  """Unload a config entry."""
143  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
144  del hass.data[DOMAIN][entry.entry_id]
145  return unload_ok
None __init__(self, SoundTouchDevice device)
Definition: __init__.py:53
def service_handle(HomeAssistant hass)
Definition: __init__.py:219
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:59
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:141
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:131