1 """Support for interacting with Snapcast clients."""
3 from __future__
import annotations
5 from snapcast.control.server
import Snapserver
6 import voluptuous
as vol
10 MediaPlayerEntityFeature,
35 "idle": MediaPlayerState.IDLE,
36 "playing": MediaPlayerState.PLAYING,
42 """Register snapcast services."""
43 platform = entity_platform.async_get_current_platform()
45 platform.async_register_entity_service(SERVICE_SNAPSHOT,
None,
"snapshot")
46 platform.async_register_entity_service(SERVICE_RESTORE,
None,
"async_restore")
47 platform.async_register_entity_service(
48 SERVICE_JOIN, {vol.Required(ATTR_MASTER): cv.entity_id}, handle_async_join
50 platform.async_register_entity_service(SERVICE_UNJOIN,
None, handle_async_unjoin)
51 platform.async_register_entity_service(
53 {vol.Required(ATTR_LATENCY): cv.positive_int},
60 config_entry: ConfigEntry,
61 async_add_entities: AddEntitiesCallback,
63 """Set up the snapcast config entry."""
64 snapcast_server: Snapserver = hass.data[DOMAIN][config_entry.entry_id].server
68 host = config_entry.data[CONF_HOST]
69 port = config_entry.data[CONF_PORT]
70 hpid = f
"{host}:{port}"
72 groups: list[MediaPlayerEntity] = [
74 for group
in snapcast_server.groups
76 clients: list[MediaPlayerEntity] = [
78 for client
in snapcast_server.clients
83 ].hass_async_add_entities = async_add_entities
87 """Handle the entity service join."""
88 if not isinstance(entity, SnapcastClientDevice):
89 raise TypeError(
"Entity is not a client. Can only join clients.")
90 await entity.async_join(service_call.data[ATTR_MASTER])
94 """Handle the entity service unjoin."""
95 if not isinstance(entity, SnapcastClientDevice):
96 raise TypeError(
"Entity is not a client. Can only unjoin clients.")
97 await entity.async_unjoin()
101 """Handle the entity service set_latency."""
102 if not isinstance(entity, SnapcastClientDevice):
103 raise TypeError(
"Latency can only be set for a Snapcast client.")
104 await entity.async_set_latency(service_call.data[ATTR_LATENCY])
108 """Representation of a Snapcast group device."""
110 _attr_should_poll =
False
111 _attr_supported_features = (
112 MediaPlayerEntityFeature.VOLUME_MUTE
113 | MediaPlayerEntityFeature.VOLUME_SET
114 | MediaPlayerEntityFeature.SELECT_SOURCE
118 """Initialize the Snapcast group device."""
122 self.
_attr_unique_id_attr_unique_id = f
"{GROUP_PREFIX}{uid_part}_{self._group.identifier}"
125 """Subscribe to group events."""
127 self.
hasshass.data[DOMAIN][self.
_entry_id_entry_id].groups.append(self)
130 """Disconnect group object when removed."""
131 self.
_group_group.set_callback(
None)
132 self.
hasshass.data[DOMAIN][self.
_entry_id_entry_id].groups.remove(self)
135 """Set availability of group."""
140 def state(self) -> MediaPlayerState | None:
141 """Return the state of the player."""
143 return MediaPlayerState.IDLE
144 return STREAM_STATUS.get(self.
_group_group.stream_status)
148 """Return the snapcast identifier."""
149 return self.
_group_group.identifier
153 """Return the name of the device."""
154 return f
"{self._group.friendly_name} {GROUP_SUFFIX}"
158 """Return the current input source."""
159 return self.
_group_group.stream
163 """Return the volume level."""
164 return self.
_group_group.volume / 100
169 return self.
_group_group.muted
173 """List of available input sources."""
174 return list(self.
_group_group.streams_by_name().keys())
177 """Set input source."""
178 streams = self.
_group_group.streams_by_name()
179 if source
in streams:
180 await self.
_group_group.set_stream(streams[source].identifier)
184 """Send the mute command."""
185 await self.
_group_group.set_muted(mute)
189 """Set the volume level."""
190 await self.
_group_group.set_volume(round(volume * 100))
194 """Snapshot the group state."""
198 """Restore the group state."""
199 await self.
_group_group.restore()
204 """Representation of a Snapcast client device."""
206 _attr_should_poll =
False
207 _attr_supported_features = (
208 MediaPlayerEntityFeature.VOLUME_MUTE
209 | MediaPlayerEntityFeature.VOLUME_SET
210 | MediaPlayerEntityFeature.SELECT_SOURCE
214 """Initialize the Snapcast client device."""
218 self.
_attr_unique_id_attr_unique_id = f
"{CLIENT_PREFIX}{uid_part}_{self._client.identifier}"
222 """Subscribe to client events."""
224 self.
hasshass.data[DOMAIN][self.
_entry_id_entry_id].clients.append(self)
227 """Disconnect client object when removed."""
228 self.
_client_client.set_callback(
None)
229 self.
hasshass.data[DOMAIN][self.
_entry_id_entry_id].clients.remove(self)
232 """Set availability of group."""
238 """Return the snapcast identifier."""
239 return self.
_client_client.identifier
243 """Return the name of the device."""
244 return f
"{self._client.friendly_name} {CLIENT_SUFFIX}"
248 """Return the current input source."""
249 return self.
_client_client.group.stream
253 """Return the volume level."""
254 return self.
_client_client.volume / 100
259 return self.
_client_client.muted
263 """List of available input sources."""
264 return list(self.
_client_client.group.streams_by_name().keys())
267 def state(self) -> MediaPlayerState | None:
268 """Return the state of the player."""
269 if self.
_client_client.connected:
271 return MediaPlayerState.IDLE
272 return STREAM_STATUS.get(self.
_client_client.group.stream_status)
273 return MediaPlayerState.STANDBY
277 """Return the state attributes."""
279 if self.
latencylatency
is not None:
280 state_attrs[
"latency"] = self.
latencylatency
285 """Latency for Client."""
286 return self.
_client_client.latency
289 """Set input source."""
290 streams = self.
_client_client.group.streams_by_name()
291 if source
in streams:
292 await self.
_client_client.group.set_stream(streams[source].identifier)
296 """Send the mute command."""
297 await self.
_client_client.set_muted(mute)
301 """Set the volume level."""
302 await self.
_client_client.set_volume(round(volume * 100))
306 """Join the group of the master player."""
307 master_entity = next(
309 for entity
in self.
hasshass.data[DOMAIN][self.
_entry_id_entry_id].clients
310 if entity.entity_id == master
312 if not isinstance(master_entity, SnapcastClientDevice):
313 raise TypeError(
"Master is not a client device. Can only join clients.")
317 for group
in self.
_client_client.groups_available()
318 if master_entity.identifier
in group.clients
320 await master_group.add_client(self.
_client_client.identifier)
324 """Unjoin the group the player is currently in."""
325 await self.
_client_client.group.remove_client(self.
_client_client.identifier)
329 """Snapshot the client state."""
333 """Restore the client state."""
334 await self.
_client_client.restore()
338 """Set the latency of the client."""
339 await self.
_client_client.set_latency(latency)
None async_write_ha_state(self)
None schedule_update_ha_state(self, bool force_refresh=False)