How to change the scan interval of a media_player from config (and if it would be allowed)

Tags: #<Tag:0x00007fc42c4f5e28>

I would like to change the scan interval of the Spotify integration, and as such I was planning on making a pull request to allow the ability to input the scan interval (either from config (option?) flow or from YAML, but not relevant atm). However, I do not see the ability to change the interval dynamically, as it appears that all media_player integrations use a constant SCAN_INTERVAL that they define at the top of the file, which the main media_player uses during its async_setup, and I have no idea how to change it once it’s been set.

I figure that this is partially intentional, as I have seen mention that we don’t want a few users to set it to a super specific small value and overload Spotify (most recently from Frenck in the HA Discord here). However, I would counter that argument with Spotify specifically, as they have rate-limiting that would prevent such a situation; plus Spotify would most likely target the specific app (client ID + secret) in question rather than HA as a whole.

As a personal anecdote, in a separate project I have been doing a request every 0.5 seconds, resulting in ~100k requests a day (versus ~3k from HA updating every 30 seconds) for the past 1-2 years without an issue. I do understand that we can’t rely on this being allowed on a larger scale ofc, but just how it’s been for me.

I also figure that if we did want to implement this, it should be more generic than just Spotify (since hypothetically it could apply to all media_player's) but I figure that isn’t my call.

As such, I was wondering if someone could point me to the right direction on how to do so, if it would even be allowed.

It should be noted that this solution does not seem to work. E.g.:

media_player:
  - platform: spotify
    scan_interval: 10

Now imagine what would happen if everyone did this 30-fold increase in load on the API.

Home Assistant would be blocked for abusing the service.

There may be another way to to get updates when needed. What exactly are you finding is too slow?

1 Like

Now imagine what would happen if everyone did this 30-fold increase in load on the API.
Home Assistant would be blocked for abusing the service.

I understand that y’all have run into this before, but I specifically mentioned this; do you disagree with my rate-limiting counter point? I understand 0.5 seconds is super fast, but couldn’t we just set a minimum configurable value that isn’t outrageous?

There may be another way to to get updates when needed. What exactly are you finding is too slow?

Current track display, so I doubt it. It’s for displaying the currently played track on a tablet that’s at my desktop, and is supposed to be constantly in sync with it (so if I change it on my computer from the app it should update on the tablet).

If you are controlling it via Home Assistant on the tablet you could try this:

Write an automation that triggers on the stop, pause, play, skip fwd, and skip rev events.

In the actions use the homeassitant.update_entity service to update your media player.

Yeah I’m only controlling it from the tablet like 30% of the time so it’ll still be 30 seconds behind very frequently, hence the desire for changing the scan_interval. I appreciate the work around, but it won’t result in the effect I desire.

Revisting this, as the recent update blog post made me realize automations have a Time Pattern option. I did your suggested homeassistant.update_entity solution with Time Pattern and it works as I’d like. Thanks for the suggestion!