|
17 | 17 | from soco.data_structures import DidlFavorite, DidlMusicTrack |
18 | 18 | from soco.ms_data_structures import MusicServiceItem |
19 | 19 | from sonos_websocket.exception import SonosWebsocketError |
20 | | -import voluptuous as vol |
21 | 20 |
|
22 | 21 | from homeassistant.components import media_source, spotify |
23 | 22 | from homeassistant.components.media_player import ( |
|
40 | 39 | ) |
41 | 40 | from homeassistant.components.plex import PLEX_URI_SCHEME |
42 | 41 | from homeassistant.components.plex.services import process_plex_payload |
43 | | -from homeassistant.const import ATTR_TIME |
44 | | -from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse, callback |
| 42 | +from homeassistant.core import HomeAssistant, callback |
45 | 43 | from homeassistant.exceptions import HomeAssistantError, ServiceValidationError |
46 | | -from homeassistant.helpers import ( |
47 | | - config_validation as cv, |
48 | | - entity_platform, |
49 | | - entity_registry as er, |
50 | | - service, |
51 | | -) |
| 44 | +from homeassistant.helpers import entity_registry as er |
52 | 45 | from homeassistant.helpers.dispatcher import async_dispatcher_connect |
53 | 46 | from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
54 | 47 | from homeassistant.helpers.event import async_call_later |
55 | 48 |
|
56 | 49 | from . import media_browser |
57 | 50 | from .const import ( |
| 51 | + ATTR_QUEUE_POSITION, |
58 | 52 | DOMAIN, |
59 | 53 | MEDIA_TYPE_DIRECTORY, |
60 | 54 | MEDIA_TYPES_TO_SONOS, |
|
93 | 87 | UPNP_ERRORS_TO_IGNORE = ["701", "711", "712"] |
94 | 88 | ANNOUNCE_NOT_SUPPORTED_ERRORS: list[str] = ["globalError"] |
95 | 89 |
|
96 | | -SERVICE_SNAPSHOT = "snapshot" |
97 | | -SERVICE_RESTORE = "restore" |
98 | | -SERVICE_SET_TIMER = "set_sleep_timer" |
99 | | -SERVICE_CLEAR_TIMER = "clear_sleep_timer" |
100 | | -SERVICE_UPDATE_ALARM = "update_alarm" |
101 | | -SERVICE_PLAY_QUEUE = "play_queue" |
102 | | -SERVICE_REMOVE_FROM_QUEUE = "remove_from_queue" |
103 | | -SERVICE_GET_QUEUE = "get_queue" |
104 | | - |
105 | | -ATTR_SLEEP_TIME = "sleep_time" |
106 | | -ATTR_ALARM_ID = "alarm_id" |
107 | | -ATTR_VOLUME = "volume" |
108 | | -ATTR_ENABLED = "enabled" |
109 | | -ATTR_INCLUDE_LINKED_ZONES = "include_linked_zones" |
110 | | -ATTR_MASTER = "master" |
111 | | -ATTR_WITH_GROUP = "with_group" |
112 | | -ATTR_QUEUE_POSITION = "queue_position" |
113 | | - |
114 | 90 |
|
115 | 91 | async def async_setup_entry( |
116 | 92 | hass: HomeAssistant, |
117 | 93 | config_entry: SonosConfigEntry, |
118 | 94 | async_add_entities: AddConfigEntryEntitiesCallback, |
119 | 95 | ) -> None: |
120 | 96 | """Set up Sonos from a config entry.""" |
121 | | - platform = entity_platform.async_get_current_platform() |
122 | 97 |
|
123 | 98 | @callback |
124 | 99 | def async_create_entities(speaker: SonosSpeaker) -> None: |
125 | 100 | """Handle device discovery and create entities.""" |
126 | 101 | _LOGGER.debug("Creating media_player on %s", speaker.zone_name) |
127 | 102 | async_add_entities([SonosMediaPlayerEntity(speaker, config_entry)]) |
128 | 103 |
|
129 | | - @service.verify_domain_control(hass, DOMAIN) |
130 | | - async def async_service_handle(service_call: ServiceCall) -> None: |
131 | | - """Handle dispatched services.""" |
132 | | - assert platform is not None |
133 | | - entities = await platform.async_extract_from_service(service_call) |
134 | | - |
135 | | - if not entities: |
136 | | - return |
137 | | - |
138 | | - speakers = [] |
139 | | - for entity in entities: |
140 | | - assert isinstance(entity, SonosMediaPlayerEntity) |
141 | | - speakers.append(entity.speaker) |
142 | | - |
143 | | - if service_call.service == SERVICE_SNAPSHOT: |
144 | | - await SonosSpeaker.snapshot_multi( |
145 | | - hass, config_entry, speakers, service_call.data[ATTR_WITH_GROUP] |
146 | | - ) |
147 | | - elif service_call.service == SERVICE_RESTORE: |
148 | | - await SonosSpeaker.restore_multi( |
149 | | - hass, config_entry, speakers, service_call.data[ATTR_WITH_GROUP] |
150 | | - ) |
151 | | - |
152 | 104 | config_entry.async_on_unload( |
153 | 105 | async_dispatcher_connect(hass, SONOS_CREATE_MEDIA_PLAYER, async_create_entities) |
154 | 106 | ) |
155 | 107 |
|
156 | | - join_unjoin_schema = cv.make_entity_service_schema( |
157 | | - {vol.Optional(ATTR_WITH_GROUP, default=True): cv.boolean} |
158 | | - ) |
159 | | - |
160 | | - hass.services.async_register( |
161 | | - DOMAIN, SERVICE_SNAPSHOT, async_service_handle, join_unjoin_schema |
162 | | - ) |
163 | | - |
164 | | - hass.services.async_register( |
165 | | - DOMAIN, SERVICE_RESTORE, async_service_handle, join_unjoin_schema |
166 | | - ) |
167 | | - |
168 | | - platform.async_register_entity_service( |
169 | | - SERVICE_SET_TIMER, |
170 | | - { |
171 | | - vol.Required(ATTR_SLEEP_TIME): vol.All( |
172 | | - vol.Coerce(int), vol.Range(min=0, max=86399) |
173 | | - ) |
174 | | - }, |
175 | | - "set_sleep_timer", |
176 | | - ) |
177 | | - |
178 | | - platform.async_register_entity_service( |
179 | | - SERVICE_CLEAR_TIMER, None, "clear_sleep_timer" |
180 | | - ) |
181 | | - |
182 | | - platform.async_register_entity_service( |
183 | | - SERVICE_UPDATE_ALARM, |
184 | | - { |
185 | | - vol.Required(ATTR_ALARM_ID): cv.positive_int, |
186 | | - vol.Optional(ATTR_TIME): cv.time, |
187 | | - vol.Optional(ATTR_VOLUME): cv.small_float, |
188 | | - vol.Optional(ATTR_ENABLED): cv.boolean, |
189 | | - vol.Optional(ATTR_INCLUDE_LINKED_ZONES): cv.boolean, |
190 | | - }, |
191 | | - "set_alarm", |
192 | | - ) |
193 | | - |
194 | | - platform.async_register_entity_service( |
195 | | - SERVICE_PLAY_QUEUE, |
196 | | - {vol.Optional(ATTR_QUEUE_POSITION): cv.positive_int}, |
197 | | - "play_queue", |
198 | | - ) |
199 | | - |
200 | | - platform.async_register_entity_service( |
201 | | - SERVICE_REMOVE_FROM_QUEUE, |
202 | | - {vol.Optional(ATTR_QUEUE_POSITION): cv.positive_int}, |
203 | | - "remove_from_queue", |
204 | | - ) |
205 | | - |
206 | | - platform.async_register_entity_service( |
207 | | - SERVICE_GET_QUEUE, |
208 | | - None, |
209 | | - "get_queue", |
210 | | - supports_response=SupportsResponse.ONLY, |
211 | | - ) |
212 | | - |
213 | 108 |
|
214 | 109 | class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): |
215 | 110 | """Representation of a Sonos entity.""" |
|
0 commit comments