/
/
/
1"""Tests for the Home Assistant provider constants/helpers."""
2
3from __future__ import annotations
4
5import logging
6from typing import Any
7
8import pytest
9
10from music_assistant.providers.hass.constants import (
11 MediaPlayerEntityFeature,
12 parse_supported_features,
13)
14
15LOGGER = logging.getLogger("test.hass")
16
17
18@pytest.mark.parametrize(
19 ("raw_value", "expected"),
20 [
21 (
22 int(MediaPlayerEntityFeature.PLAY_MEDIA | MediaPlayerEntityFeature.VOLUME_SET),
23 MediaPlayerEntityFeature.PLAY_MEDIA | MediaPlayerEntityFeature.VOLUME_SET,
24 ),
25 # a feature flag added in a newer Home Assistant version is kept as-is
26 (
27 int(MediaPlayerEntityFeature.PLAY_MEDIA) | 1 << 40,
28 MediaPlayerEntityFeature(int(MediaPlayerEntityFeature.PLAY_MEDIA) | 1 << 40),
29 ),
30 (0, MediaPlayerEntityFeature(0)),
31 ],
32)
33def test_valid_supported_features(raw_value: int, expected: MediaPlayerEntityFeature) -> None:
34 """Values Home Assistant can report are translated to the matching feature flags."""
35 assert parse_supported_features(raw_value, "media_player.test", LOGGER) == expected
36
37
38@pytest.mark.parametrize(
39 "raw_value",
40 [
41 # an entity that (currently) has no state reports no attributes at all
42 None,
43 # integrations are free to write anything into the attribute
44 [],
45 ["PLAY_MEDIA"],
46 "512",
47 True,
48 -1,
49 ],
50)
51def test_invalid_supported_features(raw_value: Any) -> None:
52 """A value Music Assistant can not interpret is reported as 'no features supported'."""
53 assert parse_supported_features(raw_value, "media_player.test", LOGGER) == (
54 MediaPlayerEntityFeature(0)
55 )
56
57
58def test_invalid_supported_features_are_logged(caplog: pytest.LogCaptureFixture) -> None:
59 """An entity with an invalid value is named in the log so it can be traced back."""
60 parse_supported_features([], "media_player.new_receiver", LOGGER)
61 assert "media_player.new_receiver" in caplog.text
62
63
64def test_missing_supported_features_are_not_logged(caplog: pytest.LogCaptureFixture) -> None:
65 """An entity without any state is not reported as misbehaving."""
66 parse_supported_features(None, "media_player.new_receiver", LOGGER)
67 assert not caplog.text
68