Need test for BITWISE_AND

With the changes this PR introduced in 2022.4, if an operation is called for an entity that does not support it an error is raised. While this is obviously good from from a programming structure stand point, it is particularly problematic for media players.

I have a script that turns off all media players when I leave this house. I select which media players need to be turned off by iterating the states object to determine what media players are actually on, or playing.

{{ states.media_player|selectattr('state','in','on','home','idle','playing','paused','idle'])
     |map(attribute='entity_id')|list }}

In this case, if a browser_mod media player happens to be playing, this script now will now raise an error because browser players do not support media_player.turn off.

We need to be able to filter out unsupported entities when calling an operation. In the case of media players, the supported operations are noted in the supported_features attribute which is a bitfield. but unfortunately there is no bitfield_and test (however, there is a bitwise_and filter).

As a workaround I have created custom attributes for all of my existing media players to indicate support for off, pause etc so I have something to filter the entities with. This is a kludgy workaround though - customized attributes must be updated every time a media player is added/removed.

This workaround is also limited in that the attributes are not dynamic. So for example, a chormecast supports media_pause most of the time when playing media, but if you are casting a dashboard it does not and will now fail if media_pause is called.

ERROR (MainThread) [homeassistant.components.automation.media_player_auto_pause] Error while executing automation automation.media_player_auto_pause: Entity media_player.living_room_chromecast does not support this service.

We need a bitwise_and test so we can reliable select media players the support the current operation without kludgy workarounds!

{{ states.media_player|selectattr('state','in','on','home','idle','playing','paused','idle'])
    |selectattr('attributes.supported_features','bitwise_and',1)
    |map(attribute='entity_id')|list }}

Also see this comment on discord.

You can. Use |rejectattr()

OK, So what attribute would we be testing to reject media players that don’t support the media_player.turn_off service?

So in putting together reply to explain more clearly what I’m talking about, I’ve actually come up with a slightly less kludgy way to filter media players instead of using custom attributes (which I probably should have thought of in the first place), but the request for a bitwise_and (and bitwise_or) test availability is still quiet valid, IMO.

I have a script that turns off all media players that are not already off a certain times (night, leave the house etc). Prior to v2022.4 we could select those media players like this.

{{ states.media_player
     |selectattr('state','in',['on','home','playing','paused'])
     |map(attribute='entity_id')|list }}

Because I have a handful of media players that don’t support the turn_off service, the service call now throws an error because those players are included. So now we must filter out those media players.

If bitwise_and was available as a test we could do this to filter media players that do not support media_player.turn_off.

{{ states.media_player
    |selectattr('attributes.supported_features','bitwise_and',256)
    |selectattr('state','in',['on','home','playing','paused'])
    |map(attribute='entity_id')|list }}

Since bitwise_and is a filter, we can do this.

{% set players = namespace(entity_id=[]) %}
{% for item in states.media_player|selectattr('state','in',['on','home','playing','paused']) %}
  {% if item.attributes.supported_features|bitwise_and(256) %}
    {% set players.entity_id = players.entity_id + [item.entity_id] %}
  {% endif %}
{% endfor %}
{{ players.entity_id }}

Obviously having it available as a test would be more efficient, hence the request. I’m actually surprised that nobody else besides me has run across this issue.

For reference:

Media Player Features Enum Values

PAUSE = 1
SEEK = 2
VOLUME_SET = 4
VOLUME_MUTE = 8
PREVIOUS_TRACK = 16
NEXT_TRACK = 32

TURN_ON = 128
TURN_OFF = 256
PLAY_MEDIA = 512
VOLUME_STEP = 1024
SELECT_SOURCE = 2048
STOP = 4096
CLEAR_PLAYLIST = 8192
PLAY = 16384
SHUFFLE_SET = 32768
SELECT_SOUND_MODE = 65536
BROWSE_MEDIA = 131072
REPEAT_SET = 262144
GROUPING = 524288
3 Likes