Add Twitch stream viewer as readable data for integration (#128787)

This commit is contained in:
LunaBytesBack 2024-10-20 16:07:43 +02:00 committed by GitHub
parent 0c6a640e50
commit 711c448242
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 7 additions and 1 deletions

View File

@ -38,6 +38,7 @@ class TwitchUpdate:
subscription_gifted: bool | None
follows: bool
following_since: datetime | None
viewers: int | None
class TwitchCoordinator(DataUpdateCoordinator[dict[str, TwitchUpdate]]):
@ -112,5 +113,6 @@ class TwitchCoordinator(DataUpdateCoordinator[dict[str, TwitchUpdate]]):
sub.is_gift 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,
)
return data

View File

@ -24,6 +24,7 @@ ATTR_FOLLOW = "following"
ATTR_FOLLOW_SINCE = "following_since"
ATTR_FOLLOWING = "followers"
ATTR_VIEWS = "views"
ATTR_VIEWERS = "viewers"
ATTR_STARTED_AT = "started_at"
STATE_OFFLINE = "offline"
@ -82,6 +83,7 @@ class TwitchSensor(CoordinatorEntity[TwitchCoordinator], SensorEntity):
ATTR_GAME: channel.game,
ATTR_TITLE: channel.title,
ATTR_STARTED_AT: channel.started_at,
ATTR_VIEWERS: channel.viewers,
}
resp[ATTR_SUBSCRIPTION] = False
if channel.subscribed is not None:

View File

@ -3,6 +3,7 @@
"game_name": "Good game",
"title": "Title",
"thumbnail_url": "stream-medium.png",
"started_at": "2021-03-10T03:18:11Z"
"started_at": "2021-03-10T03:18:11Z",
"viewer_count": 42
}
]

View File

@ -45,6 +45,7 @@ async def test_streaming(
assert sensor_state.attributes["started_at"] == datetime(
year=2021, month=3, day=10, hour=3, minute=18, second=11, tzinfo=tzutc()
)
assert sensor_state.attributes["viewers"] == 42
async def test_oauth_without_sub_and_follow(