Expose Twitch "Started At" attribute (#121265)

This commit is contained in:
Devin 2024-07-05 17:33:04 -03:00 committed by GitHub
parent f2c9188e46
commit 9d204613e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 1 deletions

View File

@ -28,6 +28,7 @@ ATTR_FOLLOW = "following"
ATTR_FOLLOW_SINCE = "following_since" ATTR_FOLLOW_SINCE = "following_since"
ATTR_FOLLOWING = "followers" ATTR_FOLLOWING = "followers"
ATTR_VIEWS = "views" ATTR_VIEWS = "views"
ATTR_STARTED_AT = "started_at"
STATE_OFFLINE = "offline" STATE_OFFLINE = "offline"
STATE_STREAMING = "streaming" STATE_STREAMING = "streaming"
@ -104,6 +105,7 @@ class TwitchSensor(SensorEntity):
self._attr_native_value = STATE_STREAMING self._attr_native_value = STATE_STREAMING
self._attr_extra_state_attributes[ATTR_GAME] = stream.game_name self._attr_extra_state_attributes[ATTR_GAME] = stream.game_name
self._attr_extra_state_attributes[ATTR_TITLE] = stream.title self._attr_extra_state_attributes[ATTR_TITLE] = stream.title
self._attr_extra_state_attributes[ATTR_STARTED_AT] = stream.started_at
self._attr_entity_picture = stream.thumbnail_url self._attr_entity_picture = stream.thumbnail_url
if self._attr_entity_picture is not None: if self._attr_entity_picture is not None:
self._attr_entity_picture = self._attr_entity_picture.format( self._attr_entity_picture = self._attr_entity_picture.format(
@ -114,6 +116,7 @@ class TwitchSensor(SensorEntity):
self._attr_native_value = STATE_OFFLINE self._attr_native_value = STATE_OFFLINE
self._attr_extra_state_attributes[ATTR_GAME] = None self._attr_extra_state_attributes[ATTR_GAME] = None
self._attr_extra_state_attributes[ATTR_TITLE] = None self._attr_extra_state_attributes[ATTR_TITLE] = None
self._attr_extra_state_attributes[ATTR_STARTED_AT] = None
self._attr_entity_picture = self._channel.profile_image_url self._attr_entity_picture = self._channel.profile_image_url
async def _async_add_user_attributes(self) -> None: async def _async_add_user_attributes(self) -> None:

View File

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

View File

@ -3,6 +3,7 @@
from datetime import datetime from datetime import datetime
from unittest.mock import AsyncMock from unittest.mock import AsyncMock
from dateutil.tz import tzutc
from twitchAPI.object.api import FollowedChannel, Stream, UserSubscription from twitchAPI.object.api import FollowedChannel, Stream, UserSubscription
from twitchAPI.type import TwitchResourceNotFound from twitchAPI.type import TwitchResourceNotFound
@ -41,6 +42,9 @@ async def test_streaming(
assert sensor_state.attributes["entity_picture"] == "stream-medium.png" assert sensor_state.attributes["entity_picture"] == "stream-medium.png"
assert sensor_state.attributes["game"] == "Good game" assert sensor_state.attributes["game"] == "Good game"
assert sensor_state.attributes["title"] == "Title" assert sensor_state.attributes["title"] == "Title"
assert sensor_state.attributes["started_at"] == datetime(
year=2021, month=3, day=10, hour=3, minute=18, second=11, tzinfo=tzutc()
)
async def test_oauth_without_sub_and_follow( async def test_oauth_without_sub_and_follow(