Skip to content

Commit

Permalink
[ 1.0.10 ] - Updated initialization method to load the _attr_source_l…
Browse files Browse the repository at this point in the history
…ist state property with the list of sources the device supports. Also added listener to update the source list when it is changed in real-time.
  • Loading branch information
thlucas1 committed Nov 9, 2023
1 parent 6519cb3 commit 207ca81
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Change are listed in reverse chronological order (newest to oldest).

<span class="changelog">

###### [ 1.0.10 ] - 2023/11/08

* Updated initialization method to load the _attr_source_list state property with the list of sources the device supports. Also added listener to update the source list when it is changed in real-time.

###### [ 1.0.9 ] - 2023/11/06

* Allow user to disable websocket notifications if desired when device is added. This will enable device polling every 10 seconds from Home Assistant UI for status updates.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/soundtouchplus/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def validate_device_connection_http(hass:HomeAssistant, data:dict) -> dict
}

except Exception as ex:
# return self.async_abort(reason="cannot_connect")

raise CannotConnect from ex


Expand Down
4 changes: 2 additions & 2 deletions custom_components/soundtouchplus/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"websocket-client>=1.6.4",
"urllib3",
"smartinspectPython>=3.0.27",
"bosesoundtouchapi>=1.0.6"
"bosesoundtouchapi>=1.0.9"
],
"version": "1.0.9",
"version": "1.0.10",
"zeroconf": [ "_soundtouch._tcp.local." ]
}
50 changes: 42 additions & 8 deletions custom_components/soundtouchplus/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ async def async_added_to_hass(self) -> None:
The call back registration is done once this entity is registered with Home
Assistant (rather than in the `__init__` method).
"""
# load list of supported sources.
_logsi.LogVerbose("'%s': loading list of sources that the device supports" % (self.name))
sourceList:SourceList = await self.hass.async_add_executor_job(self._client.GetSourceList, True)
self._attr_source_list = sourceList.ToSourceArray(True)
_logsi.LogVerbose("'%s': _attr_source_list = %s" % (self.name, str(self._attr_source_list)))

# if websocket support is disabled then we are done at this point.
if self._socket is None:
return

Expand All @@ -156,6 +163,7 @@ async def async_added_to_hass(self) -> None:
# add our listener(s) that will handle SoundTouch device status updates.
self._socket.AddListener(SoundTouchNotifyCategorys.nowPlayingUpdated, self._OnSoundTouchUpdateEvent_nowPlayingUpdated)
# self._socket.AddListener(SoundTouchNotifyCategorys.nowSelectionUpdated, self.OnSoundTouchUpdateEvent)
self._socket.AddListener(SoundTouchNotifyCategorys.sourcesUpdated, self._OnSoundTouchUpdateEvent_sourcesUpdated)
self._socket.AddListener(SoundTouchNotifyCategorys.volumeUpdated, self._OnSoundTouchUpdateEvent_volumeUpdated)
self._socket.AddListener(SoundTouchNotifyCategorys.zoneUpdated, self._OnSoundTouchUpdateEvent_zoneUpdated)
#self._socket.AddListener(SoundTouchNotifyCategorys.groupUpdated, self._OnSoundTouchUpdateEvent_groupUpdated)
Expand Down Expand Up @@ -190,6 +198,7 @@ async def async_will_remove_from_hass(self) -> None:
if self._socket is not None:
_logsi.LogVerbose("'%s': async_will_remove_from_hass is stopping websocket notifications" % (self.name))
self._socket.StopNotification()
self._socket.ClearListeners()
self._socket = None


Expand Down Expand Up @@ -684,9 +693,9 @@ def _OnSoundTouchInfoEvent(self, client:SoundTouchClient, args:Element) -> None:
self.async_write_ha_state()

@callback
def _OnSoundTouchUpdateEvent_volumeUpdated(self, client:SoundTouchClient, args:Element) -> None:
def _OnSoundTouchUpdateEvent_nowPlayingUpdated(self, client:SoundTouchClient, args:Element) -> None:
"""
Process a volumeUpdated event notification from the SoundTouch device.
Process a nowPlayingUpdated event notification from the SoundTouch device.
"""
if (args != None):

Expand All @@ -696,18 +705,18 @@ def _OnSoundTouchUpdateEvent_volumeUpdated(self, client:SoundTouchClient, args:E
_logsi.LogXml(SILevel.Verbose, "'%s': event notification - %s" % (client.Device.DeviceName, args.tag), argsEncoded)

# create configuration model from update event argument and update the cache.
config:Volume = Volume(root=args[0])
client.ConfigurationCache[SoundTouchNodes.volume.Path] = config
config:NowPlayingStatus = NowPlayingStatus(root=args[0])
client.ConfigurationCache[SoundTouchNodes.nowPlaying.Path] = config

# inform Home Assistant of the status update.
self.update()
self.async_write_ha_state()


@callback
def _OnSoundTouchUpdateEvent_nowPlayingUpdated(self, client:SoundTouchClient, args:Element) -> None:
def _OnSoundTouchUpdateEvent_sourcesUpdated(self, client:SoundTouchClient, args:Element) -> None:
"""
Process a nowPlayingUpdated event notification from the SoundTouch device.
Process a sourcesUpdated event notification from the SoundTouch device.
"""
if (args != None):

Expand All @@ -717,8 +726,33 @@ def _OnSoundTouchUpdateEvent_nowPlayingUpdated(self, client:SoundTouchClient, ar
_logsi.LogXml(SILevel.Verbose, "'%s': event notification - %s" % (client.Device.DeviceName, args.tag), argsEncoded)

# create configuration model from update event argument and update the cache.
config:NowPlayingStatus = NowPlayingStatus(root=args[0])
client.ConfigurationCache[SoundTouchNodes.nowPlaying.Path] = config
config:SourceList = SourceList(root=args[0])
client.ConfigurationCache[SoundTouchNodes.sources.Path] = config

# update HA UI source list as well.
self._attr_source_list = config.ToSourceArray(True)
_logsi.LogVerbose("'%s': _attr_source_list updated = %s" % (self.name, str(self._attr_source_list)))

# inform Home Assistant of the status update.
self.update()
self.async_write_ha_state()


@callback
def _OnSoundTouchUpdateEvent_volumeUpdated(self, client:SoundTouchClient, args:Element) -> None:
"""
Process a volumeUpdated event notification from the SoundTouch device.
"""
if (args != None):

if (_logsi.IsOn(SILevel.Verbose)):
ElementTree.indent(args) # for pretty printing
argsEncoded = ElementTree.tostring(args, encoding="unicode")
_logsi.LogXml(SILevel.Verbose, "'%s': event notification - %s" % (client.Device.DeviceName, args.tag), argsEncoded)

# create configuration model from update event argument and update the cache.
config:Volume = Volume(root=args[0])
client.ConfigurationCache[SoundTouchNodes.volume.Path] = config

# inform Home Assistant of the status update.
self.update()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ homeassistant==2023.8.0
pip>=21.0,<23.4
ruff==0.1.3
smartinspectPython>=3.0.27
bosesoundtouchapi>=1.0.6
bosesoundtouchapi>=1.0.9

0 comments on commit 207ca81

Please sign in to comment.