Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TTS actions fail sometimes with Snapcast because of KeyError #2979

Open
1 task done
freakshock88 opened this issue Sep 29, 2024 · 12 comments
Open
1 task done

TTS actions fail sometimes with Snapcast because of KeyError #2979

freakshock88 opened this issue Sep 29, 2024 · 12 comments

Comments

@freakshock88
Copy link

freakshock88 commented Sep 29, 2024

What version of Music Assistant has the issue?

2.3.0b27

What version of the Home Assistant Integration have you got installed?

2024.9.1

Have you tried everything in the Troubleshooting FAQ and reviewed the Open and Closed Issues and Discussions to resolve this yourself?

  • Yes

The problem

WHen using TTS services like tts.speak or tts.cloud_say this sometimes works, and sometimes fails with a KeyError message in the log.
In some cases a small part of the announcement chime can be heard, sometimes not at all.

How to reproduce

Using external snapcast server:
From developer actions perform the following action:

      - action: tts.cloud_say
        data:
          message: "Test bericht"
          language: nl-NL
          entity_id: "media_player.pi_kitchen"

Music Providers

  • tts.speak
  • tts.cloud_say

Player Providers

Using the non-bundled external snapcast server addon: https://github.com/Art-Ev/addon-snapserver.

Full log output

Relevant part of MA log:

2024-09-29 16:12:03.954 INFO (MainThread) [music_assistant.streams] Start Queue Flow stream for Queue pi-kitchen - crossfade: False
2024-09-29 16:12:04.197 INFO (MainThread) [music_assistant.streams] Finished Queue Flow stream for Queue pi-kitchen
2024-09-29 16:12:25.495 INFO (MainThread) [music_assistant.streams] Start Queue Flow stream for Queue pi-kitchen - crossfade: False
2024-09-29 16:12:33.448 INFO (MainThread) [music_assistant.players] Playback announcement to player pi-kitchen (with pre-announce: True): http://192.168.1.109:8123/api/tts_proxy/eff31259f48d074b591698ccd947d46d6883e7ee_nl-nl_6e8b81ac47_cloud.mp3

Additional information

This issue was not there in previous (not sure exactly what versions) versions of MA. I have used TTS successfully with exactly the same setup before, leading me te believe a change in the MA beta's caused this behavior somehow.

HA Log:

Logger: homeassistant
Source: components/snapcast/media_player.py:144
First occurred: 3:53:38 PM (26 occurrences)
Last logged: 4:12:39 PM

    Error doing job: Task exception was never retrieved (None)
    Error doing job: Fatal error: protocol.data_received() call failed. (None)
    Error doing job: Exception in callback Entity._async_write_ha_state_from_call_soon_threadsafe() (None)

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 960, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
    self.__async_calculate_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/snapcast/media_player.py", line 144, in state
    return STREAM_STATUS.get(self._group.stream_status)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/snapcast/control/group.py", line 54, in stream_status
    return self._server.stream(self.stream).status
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/snapcast/control/server.py", line 272, in stream
    return self._streams[stream_identifier]
           ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'MusicAssistant--5031'

What version of Home Assistant Core are your running

2024.9.3

What type of installation are you running?

Home Assistant OS

On what type of hardware are you running?

Generic x86-64 (e.g. Intel NUC)

@OzGav
Copy link
Contributor

OzGav commented Sep 29, 2024

@SantiagoSotoC

@SantiagoSotoC
Copy link

But you are using the home assistant snapcast integration for TTL ?

@freakshock88
Copy link
Author

I am indeed using the default home assistant snapcast integration. The media_player that I'm using for my TTS action is the snapcast media_player entity that Music Assistant created:
Screenshot_20240930_074645_Home Assistant

@OzGav
Copy link
Contributor

OzGav commented Sep 30, 2024

Ok you can’t do that. If you want to use Music Assistant and its features then you only use MA. That means using the MA entities and MA service calls for announcments

@freakshock88
Copy link
Author

The docs say that the TTS action is supported? See the bottom of this page:
https://music-assistant.io/faq/massannounce/?h=tts
Also, it has worked fine for me for many months..

@OzGav
Copy link
Contributor

OzGav commented Sep 30, 2024

Ok sorry there is confusion here. Santiago asked if you were using the “HA Snapcast Integration” and you said yes. Why did you say you are using that but then say you are using the MA entities?
I also see you are using an external Snapcast Server. If you do have the HA integration installed then the question is why? Additionally, have you tried testing with the HA integration disabled?

@SantiagoSotoC
Copy link

It is not a problem to have snapcast integration in ha. But modifying streams by hand can be.

I was asking because in one of the logs you post it looks like you are using the media player of the snapcast integration in home assistant.

I'm going to test if TTl works in the latest beta

@freakshock88
Copy link
Author

I'm only using the MA generated entities for the TTS actions, not the entities from the snapcast integration.

I just did some more testing, and the same KeyError also happens when using the mass.play_announcement service, on some files only! WTF..

This one works every single time (even though the volume seems to be set to 15 rather than 5, maybe some caching going on?):

action: mass.play_announcement
target:
  entity_id: media_player.pi_kitchen
data:
  url: https://homeassistant.mydomain.com/local/sounds/merol.mp3
  use_pre_announce: true
  announce_volume: 5

And this one never works, even though the file is there:

action: mass.play_announcement
target:
  entity_id: media_player.pi_kitchen
data:
  url: https://homeassistant.mydomain.com/local/sounds/triple_airhorn.mp3
  use_pre_announce: true
  announce_volume: 5

With again the same error on the second file:

Logger: homeassistant
Source: components/snapcast/media_player.py:272
First occurred: 12:38:06 PM (43 occurrences)
Last logged: 10:47:44 PM
Error doing job: Task exception was never retrieved (None)

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 960, in async_update_ha_state
  self._async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
  self.__async_calculate_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
  state = self._stringify_state(available)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state
  if (state := self.state) is None:
               ^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/snapcast/media_player.py", line 272, in state
  return STREAM_STATUS.get(self._client.group.stream_status)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/snapcast/control/group.py", line 54, in stream_status
  return self._server.stream(self.stream).status
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/snapcast/control/server.py", line 272, in stream
  return self._streams[stream_identifier]
         ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'MusicAssistant--5087'

@OzGav
Copy link
Contributor

OzGav commented Oct 1, 2024

Sorry I can't recreate this. If it isn't too big can you share the airhorn file or supply the URL where it came from so I can try that. I have tried text messages and audio files via URL to single and grouped Snacast players and that worked fine.

@OzGav OzGav added the More info label Oct 1, 2024
@SantiagoSotoC
Copy link

This line in the home assistant log looks very strange to me:

Source: components/snapcast/media_player.py

@OzGav
Copy link
Contributor

OzGav commented Oct 1, 2024

You are right. That is a HA error not MA. @freakshock88 Are you sure media_player.pi_kitchen is the MA entity?

@freakshock88
Copy link
Author

Yes I'm pretty sure ;)
See image:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants