Add subscription tier attribute to Twitch integration. (#128870)

* Add subscription tier to Twitch integration.

* Add test for Twitch tiers.  Tests do not currently pass, so this is only theoretical.

* Fix variable type

* Show tier levels as 1,2,3 instead of the raw API values of 1000,2000,3000.

* Make Twitch subscription tier fixtures strings.

* Use proper assertion value for subscription tier test.

Edited on a bus on my phone. 😎

* Update homeassistant/components/twitch/coordinator.py

* Update tests/components/twitch/test_sensor.py

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Jason Parker 2024-10-21 15:54:10 -04:00 committed by GitHub
parent 63582bb489
commit d21b8166f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 9 additions and 3 deletions

View file

@ -36,6 +36,7 @@ class TwitchUpdate:
picture: str
subscribed: bool | None
subscription_gifted: bool | None
subscription_tier: int | None
follows: bool
following_since: datetime | None
viewers: int | None
@ -111,6 +112,7 @@ class TwitchCoordinator(DataUpdateCoordinator[dict[str, TwitchUpdate]]):
channel.profile_image_url,
sub is not None if sub else None,
sub.is_gift if sub else None,
{"1000": 1, "2000": 2, "3000": 3}.get(sub.tier) if sub else None,
follows is not None and follows.total > 0,
follows.data[0].followed_at if follows and follows.total else None,
stream.viewer_count if stream else None,

View file

@ -18,8 +18,8 @@ from .coordinator import TwitchUpdate
ATTR_GAME = "game"
ATTR_TITLE = "title"
ATTR_SUBSCRIPTION = "subscribed"
ATTR_SUBSCRIPTION_SINCE = "subscribed_since"
ATTR_SUBSCRIPTION_GIFTED = "subscription_is_gifted"
ATTR_SUBSCRIPTION_TIER = "subscription_tier"
ATTR_FOLLOW = "following"
ATTR_FOLLOW_SINCE = "following_since"
ATTR_FOLLOWING = "followers"
@ -89,6 +89,7 @@ class TwitchSensor(CoordinatorEntity[TwitchCoordinator], SensorEntity):
if channel.subscribed is not None:
resp[ATTR_SUBSCRIPTION] = channel.subscribed
resp[ATTR_SUBSCRIPTION_GIFTED] = channel.subscription_gifted
resp[ATTR_SUBSCRIPTION_TIER] = channel.subscription_tier
resp[ATTR_FOLLOW] = channel.follows
if channel.follows:
resp[ATTR_FOLLOW_SINCE] = channel.following_since

View file

@ -1,3 +1,4 @@
{
"is_gift": true
"is_gift": true,
"tier": "2000"
}

View file

@ -1,3 +1,4 @@
{
"is_gift": false
"is_gift": false,
"tier": "1000"
}

View file

@ -80,6 +80,7 @@ async def test_oauth_with_sub(
sensor_state = hass.states.get(ENTITY_ID)
assert sensor_state.attributes["subscribed"] is True
assert sensor_state.attributes["subscription_is_gifted"] is False
assert sensor_state.attributes["subscription_tier"] == 1
assert sensor_state.attributes["following"] is False