From d21b8166f09d476954a1d9a77a93b2f3c8da9868 Mon Sep 17 00:00:00 2001 From: Jason Parker Date: Mon, 21 Oct 2024 15:54:10 -0400 Subject: [PATCH] Add subscription tier attribute to Twitch integration. (#128870) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- homeassistant/components/twitch/coordinator.py | 2 ++ homeassistant/components/twitch/sensor.py | 3 ++- tests/components/twitch/fixtures/check_user_subscription.json | 3 ++- .../components/twitch/fixtures/check_user_subscription_2.json | 3 ++- tests/components/twitch/test_sensor.py | 1 + 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/twitch/coordinator.py b/homeassistant/components/twitch/coordinator.py index b8d19750778..5e3de4c4ec8 100644 --- a/homeassistant/components/twitch/coordinator.py +++ b/homeassistant/components/twitch/coordinator.py @@ -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, diff --git a/homeassistant/components/twitch/sensor.py b/homeassistant/components/twitch/sensor.py index 66ca7a4445d..49195d48638 100644 --- a/homeassistant/components/twitch/sensor.py +++ b/homeassistant/components/twitch/sensor.py @@ -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 diff --git a/tests/components/twitch/fixtures/check_user_subscription.json b/tests/components/twitch/fixtures/check_user_subscription.json index b1b2a3d852a..5e710b72699 100644 --- a/tests/components/twitch/fixtures/check_user_subscription.json +++ b/tests/components/twitch/fixtures/check_user_subscription.json @@ -1,3 +1,4 @@ { - "is_gift": true + "is_gift": true, + "tier": "2000" } diff --git a/tests/components/twitch/fixtures/check_user_subscription_2.json b/tests/components/twitch/fixtures/check_user_subscription_2.json index 94d56c5ee12..38a1f063f96 100644 --- a/tests/components/twitch/fixtures/check_user_subscription_2.json +++ b/tests/components/twitch/fixtures/check_user_subscription_2.json @@ -1,3 +1,4 @@ { - "is_gift": false + "is_gift": false, + "tier": "1000" } diff --git a/tests/components/twitch/test_sensor.py b/tests/components/twitch/test_sensor.py index 60024268a68..0f7ea0c33eb 100644 --- a/tests/components/twitch/test_sensor.py +++ b/tests/components/twitch/test_sensor.py @@ -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