thlucas1 / homeassistantcomponent_spotifyplus

Home Assistant integration for Spotify Player control, services, and soundtouchplus integration support.
MIT License
56 stars 4 forks source link

Receiving Error #10

Closed jacobgrillo closed 6 months ago

jacobgrillo commented 6 months ago

System Health details

System Information

version core-2024.5.0b1
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.12.2
os_name Linux
os_version 6.6.28-haos
arch aarch64
timezone America/New_York
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok GitHub API Calls Remaining | 4956 Installed Version | 1.34.0 Stage | running Available Repositories | 1401 Downloaded Repositories | 19 HACS Data | ok
Home Assistant Cloud logged_in | true -- | -- subscription_expiration | May 6, 2024 at 8:00 PM relayer_connected | true relayer_region | us-east-1 remote_enabled | true remote_connected | true alexa_enabled | true google_enabled | true remote_server | us-east-1-9.ui.nabu.casa certificate_status | ready instance_id | f3803782778b465d8a11630f0c3822b6 can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Home Assistant Supervisor host_os | Home Assistant OS 12.3.rc1 -- | -- update_channel | beta supervisor_version | supervisor-2024.04.4 agent_version | 1.6.0 docker_version | 25.0.5 disk_total | 28.0 GB disk_used | 6.1 GB healthy | true supported | true board | green supervisor_api | ok version_api | ok installed_addons | Terminal & SSH (9.13.0), AirCast (4.2.1), File editor (5.8.0), Home Assistant Google Drive Backup (0.112.1)
Dashboards dashboards | 6 -- | -- resources | 9 views | 20 mode | storage
Recorder oldest_recorder_run | April 19, 2024 at 8:04 PM -- | -- current_recorder_run | April 28, 2024 at 4:56 PM estimated_db_size | 31.95 MiB database_engine | sqlite database_version | 3.44.2
SpotifyPlus api_endpoint_reachable | ok -- | --

Checklist

Describe the issue

Failed to call service media_player/media_play. Detected that custom integration 'spotifyplus' calls async_write_ha_state from a thread at custom_components/spotifyplus/media_player.py, line 482: self.async_write_ha_state(). Please report it to the author of the 'spotifyplus' custom integration.

Reproduction steps

1. 2. 3. ... Try to use the component on HA

Debug logs

Failed to call service media_player/media_play. Detected that custom integration 'spotifyplus' calls async_write_ha_state from a thread at custom_components/spotifyplus/media_player.py, line 482: self.async_write_ha_state(). Please report it to the author of the 'spotifyplus' custom integration.

Diagnostics dump

No response

thlucas1 commented 6 months ago

Looks like an issue with the latest release of HA core. I will look into it.

thlucas1 commented 6 months ago

@jacobgrillo I was not able to reproduce this issue in the current core-2024.4.4 release.

I just noticed that you are running a core-2024.5.0b1 release - is this some sort of beta release maybe? If so, how can I install it? I am not familiar with installing HA beta releases.

This is going to be an issue for this integration (as well as others I wrote) if indeed the call to async_write_ha_state is no longer allowed in the main media_player methods! I think the only other alternative is to issue a call to async_schedule_update_ha_state(force_refresh=True) method instead.

jacobgrillo commented 6 months ago

i found out that the issue only happens when trying to control the media through the spotify plus entity, but it works fine using the regular spotify entity from HA.

thlucas1 commented 6 months ago

@jacobgrillo That sounds about right, as the Spotify entity from HA does not perform any state updates from the various media control services.

I believe I am going to wait for the 2024.5 alpha (non-beta) release before making changes to account for this, as it may be a temporary thing that the HA developers put in the beta code. I will keep you posted.

legoped commented 6 months ago

Getting the same error in version 2024.5.0

thlucas1 commented 6 months ago

Thanks - installing the 2024.5 release now. Will keep you posted.

thlucas1 commented 6 months ago

@jacobgrillo @legoped

FYI - just released v1.0.19 that fixes this issue. I will keep the issue open for a couple of days just in case - please let me know if you have issues. Thanks!

legoped commented 6 months ago

Seems resolved. Thanks

thlucas1 commented 6 months ago

@jacobgrillo @legoped Thanks for confirming - closing the issue.

bdraco commented 6 months ago

schedule_update_ha_state is the the sync equivalent to async_write_ha_state

You probably don't want force_refresh=True unless you need it as it will make the update slower

https://developers.home-assistant.io/docs/asyncio_thread_safety#async_write_ha_state

bdraco commented 6 months ago

For reference schedule_update_ha_state is a simple wrapper that uses loop.call_soon_threadsafe to call async_write_ha_state If you use force_refresh=True, its going to do a whole update cycle on the entity.

https://github.com/home-assistant/core/blob/2590db1b6dc253e4d80b3fada5b051512be1b21a/homeassistant/helpers/entity.py#L1226 https://github.com/home-assistant/core/blob/2590db1b6dc253e4d80b3fada5b051512be1b21a/homeassistant/helpers/entity.py#L1003

thlucas1 commented 6 months ago

@bdraco Thanks for the explanation.

The issue I was having that required the async_write_ha_state was with media player controls flickering. For example, I would push pause on the media player and the icon would change to a pause icon, then change back to a play icon, then change back to a pause icon, all within less than a second. So I added the async_write_ha_state to the function where I was changing the state. That fixed the flickering issue ... but then the 2024.5 release came along and something changed in the internals to start throwing exceptions.

I can try it with the force_refresh=False and see what happens. I am tempted to leave it as-is though, as the flickering only happens when the user interacts with the player (e.g. not that often).

bdraco commented 6 months ago

If it worked before with async_write_ha_state, False will work fine and is a lot more efficient

For some context, calling the async API from another thread is a non thread safe operation which can result in crashing Home Assistant and corrupting state. The checks in 2024.5.x are quite minimal and will only catch some of the most common issues.

Please see the tip on debug mode: https://developers.home-assistant.io/docs/asyncio_thread_safety

thlucas1 commented 6 months ago

Thank you for the help! I switched all of the calls to force_refresh=False in my SpotifyPlus and SoundTouchPlus integrations, and they seem to be working as expected. I will run some more extensive tests tomorrow when I have time.

I appreciate the assist, as I am still feeling my way with custom integration development. The asyncio stuff is new to me as well. Thanks again!