From a44c035828b5852aed2456acd12b73ceb65b72d3 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Fri, 2 Aug 2024 12:01:27 -0600 Subject: [PATCH] Handle invalid index bug in play random track button --- README.md | 7 +++++-- custom_components/oasis_mini/helpers.py | 2 +- .../oasis_mini/pyoasismini/__init__.py | 16 ++++++++++------ custom_components/oasis_mini/strings.json | 2 +- .../oasis_mini/translations/en.json | 2 +- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3bb73a9..3914c1b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ -![Release](https://img.shields.io/github/v/release/natekspencer/hacs-oasis_mini?style=for-the-badge) +[![Release](https://img.shields.io/github/v/release/natekspencer/hacs-oasis_mini?style=for-the-badge)](https://github.com/natekspencer/hacs-oasis_mini/releases) [![Buy Me A Coffee/Beer](https://img.shields.io/badge/Buy_Me_A_☕/🍺-F16061?style=for-the-badge&logo=ko-fi&logoColor=white&labelColor=grey)](https://ko-fi.com/natekspencer) -[![hacs_badge](https://img.shields.io/badge/HACS-Custom-41BDF5.svg?style=for-the-badge)](https://github.com/hacs/integration) +[![HACS Custom](https://img.shields.io/badge/HACS-Custom-41BDF5.svg?style=for-the-badge)](https://github.com/hacs/integration) + +![Downloads](https://img.shields.io/github/downloads/natekspencer/hacs-oasis_mini/total?style=flat-square) +![Latest Downloads](https://img.shields.io/github/downloads/natekspencer/hacs-oasis_mini/latest/total?style=flat-square) diff --git a/custom_components/oasis_mini/helpers.py b/custom_components/oasis_mini/helpers.py index 2c12910..d908c1a 100755 --- a/custom_components/oasis_mini/helpers.py +++ b/custom_components/oasis_mini/helpers.py @@ -21,7 +21,7 @@ async def add_and_play_track(device: OasisMini, track: int) -> None: # Move track to next item in the playlist and then select it if (index := device.playlist.index(track)) != device.playlist_index: - if index != (_next := min(device.playlist_index + 1, len(device.playlist))): + if index != (_next := min(device.playlist_index + 1, len(device.playlist) - 1)): await device.async_move_track(index, _next) await device.async_change_track(_next) diff --git a/custom_components/oasis_mini/pyoasismini/__init__.py b/custom_components/oasis_mini/pyoasismini/__init__.py index 0c6a4f9..dbbf4c1 100644 --- a/custom_components/oasis_mini/pyoasismini/__init__.py +++ b/custom_components/oasis_mini/pyoasismini/__init__.py @@ -176,12 +176,16 @@ class OasisMini: async def async_add_track_to_playlist(self, track: int) -> None: """Add track to playlist.""" - if track and 0 in self.playlist: + if not track: + return + + if 0 in self.playlist: playlist = [t for t in self.playlist if t] + [track] - await self.async_set_playlist(playlist) - else: - await self._async_command(params={"ADDJOBLIST": track}) - self.playlist.append(track) + return await self.async_set_playlist(playlist) + + _LOGGER.debug("Adding track %s to playlist", track) + await self._async_command(params={"ADDJOBLIST": track}) + self.playlist.append(track) async def async_change_track(self, index: int) -> None: """Change the track.""" @@ -191,7 +195,7 @@ class OasisMini: async def async_clear_playlist(self) -> None: """Clear the playlist.""" - await self.async_set_playlist([0]) + await self.async_set_playlist([]) async def async_get_ip_address(self) -> str | None: """Get the ip address.""" diff --git a/custom_components/oasis_mini/strings.json b/custom_components/oasis_mini/strings.json index aa46b33..73576f6 100755 --- a/custom_components/oasis_mini/strings.json +++ b/custom_components/oasis_mini/strings.json @@ -26,7 +26,7 @@ "options": { "step": { "init": { - "description": "Add your cloud credentials to get additional information about your Oasis Mini", + "description": "Add your cloud credentials to get additional information about your device", "data": { "email": "[%key:common::config_flow::data::email%]", "password": "[%key:common::config_flow::data::password%]" diff --git a/custom_components/oasis_mini/translations/en.json b/custom_components/oasis_mini/translations/en.json index 4e08d81..a233920 100755 --- a/custom_components/oasis_mini/translations/en.json +++ b/custom_components/oasis_mini/translations/en.json @@ -26,7 +26,7 @@ "options": { "step": { "init": { - "description": "Add your cloud credentials to get additional information about your Oasis Mini", + "description": "Add your cloud credentials to get additional information about your device", "data": { "email": "Email", "password": "Password"