Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add video id to youtube sensor state attributes #93668

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions homeassistant/components/youtube/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ATTR_SUBSCRIBER_COUNT,
ATTR_THUMBNAIL,
ATTR_TITLE,
ATTR_VIDEO_ID,
COORDINATOR,
DOMAIN,
)
Expand All @@ -30,6 +31,7 @@ class YouTubeMixin:

value_fn: Callable[[Any], StateType]
entity_picture_fn: Callable[[Any], str]
attributes_fn: Callable[[Any], dict[str, Any]] | None


@dataclass
Expand All @@ -44,6 +46,9 @@ class YouTubeSensorEntityDescription(SensorEntityDescription, YouTubeMixin):
icon="mdi:youtube",
value_fn=lambda channel: channel[ATTR_LATEST_VIDEO][ATTR_TITLE],
entity_picture_fn=lambda channel: channel[ATTR_LATEST_VIDEO][ATTR_THUMBNAIL],
attributes_fn=lambda channel: {
ATTR_VIDEO_ID: channel[ATTR_LATEST_VIDEO][ATTR_VIDEO_ID]
},
),
YouTubeSensorEntityDescription(
key="subscribers",
Expand All @@ -52,6 +57,7 @@ class YouTubeSensorEntityDescription(SensorEntityDescription, YouTubeMixin):
native_unit_of_measurement="subscribers",
value_fn=lambda channel: channel[ATTR_SUBSCRIBER_COUNT],
entity_picture_fn=lambda channel: channel[ATTR_ICON],
attributes_fn=None,
),
]

Expand Down Expand Up @@ -84,3 +90,10 @@ def native_value(self) -> StateType:
def entity_picture(self) -> str:
"""Return the value reported by the sensor."""
return self.entity_description.entity_picture_fn(self._channel)

@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the extra state attributes."""
if self.entity_description.attributes_fn:
return self.entity_description.attributes_fn(self._channel)
return None
frenck marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions tests/components/youtube/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def test_sensor(hass: HomeAssistant, setup_integration: ComponentSetup) ->
state.attributes["entity_picture"]
== "https://i.ytimg.com/vi/wysukDrMdqU/sddefault.jpg"
)
assert state.attributes["video_id"] == "wysukDrMdqU"

state = hass.states.get("sensor.google_for_developers_subscribers")
assert state
Expand Down