1 """Support for Devialet speakers."""
3 from __future__
import annotations
5 from devialet.const
import NORMAL_INPUTS
9 MediaPlayerEntityFeature,
19 from .const
import DOMAIN, MANUFACTURER, SOUND_MODES
20 from .coordinator
import DevialetCoordinator
23 MediaPlayerEntityFeature.VOLUME_SET
24 | MediaPlayerEntityFeature.VOLUME_MUTE
25 | MediaPlayerEntityFeature.TURN_OFF
26 | MediaPlayerEntityFeature.SELECT_SOURCE
27 | MediaPlayerEntityFeature.SELECT_SOUND_MODE
30 DEVIALET_TO_HA_FEATURE_MAP = {
31 "play": MediaPlayerEntityFeature.PLAY | MediaPlayerEntityFeature.STOP,
32 "pause": MediaPlayerEntityFeature.PAUSE,
33 "previous": MediaPlayerEntityFeature.PREVIOUS_TRACK,
34 "next": MediaPlayerEntityFeature.NEXT_TRACK,
35 "seek": MediaPlayerEntityFeature.SEEK,
40 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
42 """Set up the Devialet entry."""
43 client = hass.data[DOMAIN][entry.entry_id]
45 await coordinator.async_config_entry_first_refresh()
51 CoordinatorEntity[DevialetCoordinator], MediaPlayerEntity
53 """Devialet media player."""
55 _attr_has_entity_name =
True
58 def __init__(self, coordinator: DevialetCoordinator, entry: ConfigEntry) ->
None:
59 """Initialize the Devialet device."""
66 manufacturer=MANUFACTURER,
68 name=entry.data[CONF_NAME],
69 sw_version=self.
coordinatorcoordinator.client.version,
74 """Handle updated data from the coordinator."""
75 if not self.
coordinatorcoordinator.client.is_available:
90 self.
coordinatorcoordinator.client.position_updated_at
100 def state(self) -> MediaPlayerState | None:
101 """Return the state of the device."""
102 playing_state = self.
coordinatorcoordinator.client.playing_state
104 if not playing_state:
105 return MediaPlayerState.IDLE
106 if playing_state ==
"playing":
107 return MediaPlayerState.PLAYING
108 if playing_state ==
"paused":
109 return MediaPlayerState.PAUSED
110 return MediaPlayerState.ON
114 """Return if the media player is available."""
115 return self.
coordinatorcoordinator.client.is_available
119 """Flag media player features that are supported."""
120 features = SUPPORT_DEVIALET
122 if self.
coordinatorcoordinator.client.source_state
is None:
125 if not self.
coordinatorcoordinator.client.available_options:
128 for option
in self.
coordinatorcoordinator.client.available_options:
129 features |= DEVIALET_TO_HA_FEATURE_MAP.get(option, 0)
134 """Return the current input source."""
137 for pretty_name, name
in NORMAL_INPUTS.items():
144 """Return the current sound mode."""
145 if self.
coordinatorcoordinator.client.equalizer
is not None:
146 sound_mode = self.
coordinatorcoordinator.client.equalizer
147 elif self.
coordinatorcoordinator.client.night_mode:
148 sound_mode =
"night mode"
152 for pretty_name, mode
in SOUND_MODES.items():
153 if sound_mode == mode:
158 """Volume up media player."""
159 await self.
coordinatorcoordinator.client.async_volume_up()
162 """Volume down media player."""
163 await self.
coordinatorcoordinator.client.async_volume_down()
166 """Set volume level, range 0..1."""
167 await self.
coordinatorcoordinator.client.async_set_volume_level(volume)
170 """Mute (true) or unmute (false) media player."""
171 await self.
coordinatorcoordinator.client.async_mute_volume(mute)
174 """Play media player."""
175 await self.
coordinatorcoordinator.client.async_media_play()
178 """Pause media player."""
179 await self.
coordinatorcoordinator.client.async_media_pause()
182 """Pause media player."""
183 await self.
coordinatorcoordinator.client.async_media_stop()
186 """Send the next track command."""
187 await self.
coordinatorcoordinator.client.async_media_next_track()
190 """Send the previous track command."""
191 await self.
coordinatorcoordinator.client.async_media_previous_track()
194 """Send seek command."""
195 await self.
coordinatorcoordinator.client.async_media_seek(position)
198 """Send sound mode command."""
199 for pretty_name, mode
in SOUND_MODES.items():
200 if sound_mode == pretty_name:
201 if mode ==
"night mode":
202 await self.
coordinatorcoordinator.client.async_set_night_mode(
True)
204 await self.
coordinatorcoordinator.client.async_set_night_mode(
False)
205 await self.
coordinatorcoordinator.client.async_set_equalizer(mode)
208 """Turn off media player."""
209 await self.
coordinatorcoordinator.client.async_turn_off()
212 """Select input source."""
213 await self.
coordinatorcoordinator.client.async_select_source(source)
None async_write_ha_state(self)