diff --git a/homeassistant/components/twitch/coordinator.py b/homeassistant/components/twitch/coordinator.py index 5788df7df13..b8d19750778 100644 --- a/homeassistant/components/twitch/coordinator.py +++ b/homeassistant/components/twitch/coordinator.py @@ -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 diff --git a/homeassistant/components/twitch/sensor.py b/homeassistant/components/twitch/sensor.py index 636f94114a4..66ca7a4445d 100644 --- a/homeassistant/components/twitch/sensor.py +++ b/homeassistant/components/twitch/sensor.py @@ -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: diff --git a/tests/components/twitch/fixtures/get_streams.json b/tests/components/twitch/fixtures/get_streams.json index 53330c9c82e..73f6dc1b42a 100644 --- a/tests/components/twitch/fixtures/get_streams.json +++ b/tests/components/twitch/fixtures/get_streams.json @@ -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 } ] diff --git a/tests/components/twitch/test_sensor.py b/tests/components/twitch/test_sensor.py index 8ce146adf07..60024268a68 100644 --- a/tests/components/twitch/test_sensor.py +++ b/tests/components/twitch/test_sensor.py @@ -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(