Add video id to youtube sensor state attributes (#93668)

* Add video id to state attributes

* Make extra state attributes not optional

* Revert "Make extra state attributes not optional"

This reverts commit d2f9e936c8.
pull/94158/head
Joost Lekkerkerker 2023-06-01 11:46:59 +02:00 committed by Paulus Schoutsen
parent 9aeba6221b
commit 2a99fea1de
2 changed files with 14 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from .const import (
ATTR_SUBSCRIBER_COUNT,
ATTR_THUMBNAIL,
ATTR_TITLE,
ATTR_VIDEO_ID,
COORDINATOR,
DOMAIN,
)
@ -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
@ -44,6 +46,9 @@ SENSOR_TYPES = [
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",
@ -52,6 +57,7 @@ SENSOR_TYPES = [
native_unit_of_measurement="subscribers",
value_fn=lambda channel: channel[ATTR_SUBSCRIBER_COUNT],
entity_picture_fn=lambda channel: channel[ATTR_ICON],
attributes_fn=None,
),
]
@ -84,3 +90,10 @@ class YouTubeSensor(YouTubeChannelEntity, SensorEntity):
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

View File

@ -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