Skip to content

Commit

Permalink
sonos: fix for method _play_radio (had problems with radio station na…
Browse files Browse the repository at this point in the history
…mes with spaces)
  • Loading branch information
aschwith committed Oct 7, 2023
1 parent 3a27f35 commit 2dfcff1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
60 changes: 44 additions & 16 deletions sonos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2314,7 +2314,7 @@ def _play_tunein(self, station_name: str, music_service: str = 'TuneIn', start:
# if a patch is applied.

# ------------------------------------------------------------------------------------------------------------ #
self.logger.warning(f"DEBUG: _play_radio start")
self.logger.warning(f"DEBUG: _play_tunein start")
if not self._check_property():
return False, "Property check failed"

Expand Down Expand Up @@ -2400,7 +2400,6 @@ def _play_tunein(self, station_name: str, music_service: str = 'TuneIn', start:

def _play_radio(self, station_name: str, music_service: str = 'TuneIn', start: bool = True) -> tuple:
"""
WARNING: THIS FUNCTION IS NOT WORKING FOR SOME RADIO STATIONS, e.g. with space in the name. Other names can cause infinite loops.
Plays a radio station by a given radio name at a given music service. If more than one radio station are found,
the first result will be played.
:param music_service: music service name Default: TuneIn
Expand All @@ -2424,7 +2423,7 @@ def _play_radio(self, station_name: str, music_service: str = 'TuneIn', start: b
</item>
</DIDL-Lite>'
"""

self.logger.dbghigh(f"_play_radio called with station_name= {station_name}")
# get all music services
all_music_services_names = MusicService.get_all_music_services_names()

Expand All @@ -2435,16 +2434,27 @@ def _play_radio(self, station_name: str, music_service: str = 'TuneIn', start: b
# get music service instance
music_service = MusicService(music_service)

# The following code is no longer necessary and will be removed. Its purpose was to cope with
# station names that included spaces. It seems that in the meantime, these spaces no longer
# include problems for the search
# adapt station_name for optimal search results
if " " in station_name:
station_name_for_search = station_name.split(" ", 1)[0]
elif station_name[-1].isdigit():
station_name_for_search = station_name[:-1]
else:
station_name_for_search = station_name
#if " " in station_name:
# station_name_for_search = station_name.split(" ", 1)[0]
#elif station_name[-1].isdigit():
# station_name_for_search = station_name[:-1]
#else:
# station_name_for_search = station_name
#self.logger.dbghigh(f"_play_radio: station_name_for_search = {station_name_for_search}")
#search_result = music_service.search(category='stations', term=station_name_for_search, index=0, count=100)

# do search
search_result = music_service.search(category='stations', term=station_name_for_search, index=0, count=100)
search_result = music_service.search(category='stations', term=station_name, index=0, count=100)

# Debug output of whole search list:
k=1
for station in search_result:
self.logger.dbghigh(f"Entry {k}: {station.title}")
k = k + 1

# get station object from search result
the_station = None
Expand All @@ -2455,11 +2465,11 @@ def _play_radio(self, station_name: str, music_service: str = 'TuneIn', start: b
the_station = station
break

# Fuzzy match
# Fuzzy match with lower station name:
if not the_station:
station_name = station_name.lower()
station_name_lower = station_name.lower()
for station in search_result:
if station_name in station.title.lower():
if station_name_lower in station.title.lower():
self.logger.info(f"Fuzzy match '{station.title}' found")
the_station = station
break
Expand All @@ -2468,9 +2478,27 @@ def _play_radio(self, station_name: str, music_service: str = 'TuneIn', start: b
if not the_station:
last_char = len(station_name) - 1
if station_name[last_char].isdigit():
station_name = f"{station_name[0:last_char]} {station_name[last_char:]}"
#old_station_name = f"{station_name[0:last_char]} {station_name[last_char:]}"
#self.logger.dbghigh(f"Debug: Old Very fuzzy query: {old_station_name}")

# Split Digits and Radio Name:
digitString = ''
radioNameString = ''
splitIndex = last_char
for i in reversed(range(len(station_name))):
if station_name[i].isdigit() or station_name[i] == '.' or station_name[i] == ',':
digitString = station_name[i] + digitString
else:
splitIndex = i
break
radioNameString = station_name[0:splitIndex ]
self.logger.dbghigh(f"Debug: RadioName: {radioNameString}, digitString: {digitString}")

new_station_name = f"{radioNameString.lower()} {digitString}"
self.logger.dbghigh(f"New very fuzzy query: {new_station_name}")

for station in search_result:
if station_name in station.title.lower():
if new_station_name in station.title.lower():
self.logger.info(f"Very fuzzy match '{station.title}' found")
the_station = station
break
Expand Down Expand Up @@ -2979,7 +3007,7 @@ class Sonos(SmartPlugin):
"""
Main class of the Plugin. Does all plugin specific stuff
"""
PLUGIN_VERSION = "1.8.3"
PLUGIN_VERSION = "1.8.4"

def __init__(self, sh):
"""Initializes the plugin."""
Expand Down
2 changes: 1 addition & 1 deletion sonos/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugin:
documentation: https://github.com/smarthomeNG/plugins/blob/master/sonos/README.md
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/25151-sonos-anbindung

version: 1.8.3 # Plugin version
version: 1.8.4 # Plugin version
sh_minversion: 1.5.1 # minimum shNG version to use this plugin
py_minversion: 3.8 # minimum Python version to use for this plugin
multi_instance: False # plugin supports multi instance
Expand Down

0 comments on commit 2dfcff1

Please sign in to comment.