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/93908/head
parent
a4b8b4f7c2
commit
a6b6b03f2e
|
@ -18,6 +18,7 @@ from .const import (
|
||||||
ATTR_SUBSCRIBER_COUNT,
|
ATTR_SUBSCRIBER_COUNT,
|
||||||
ATTR_THUMBNAIL,
|
ATTR_THUMBNAIL,
|
||||||
ATTR_TITLE,
|
ATTR_TITLE,
|
||||||
|
ATTR_VIDEO_ID,
|
||||||
COORDINATOR,
|
COORDINATOR,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
@ -30,6 +31,7 @@ class YouTubeMixin:
|
||||||
|
|
||||||
value_fn: Callable[[Any], StateType]
|
value_fn: Callable[[Any], StateType]
|
||||||
entity_picture_fn: Callable[[Any], str]
|
entity_picture_fn: Callable[[Any], str]
|
||||||
|
attributes_fn: Callable[[Any], dict[str, Any]] | None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -44,6 +46,9 @@ SENSOR_TYPES = [
|
||||||
icon="mdi:youtube",
|
icon="mdi:youtube",
|
||||||
value_fn=lambda channel: channel[ATTR_LATEST_VIDEO][ATTR_TITLE],
|
value_fn=lambda channel: channel[ATTR_LATEST_VIDEO][ATTR_TITLE],
|
||||||
entity_picture_fn=lambda channel: channel[ATTR_LATEST_VIDEO][ATTR_THUMBNAIL],
|
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(
|
YouTubeSensorEntityDescription(
|
||||||
key="subscribers",
|
key="subscribers",
|
||||||
|
@ -52,6 +57,7 @@ SENSOR_TYPES = [
|
||||||
native_unit_of_measurement="subscribers",
|
native_unit_of_measurement="subscribers",
|
||||||
value_fn=lambda channel: channel[ATTR_SUBSCRIBER_COUNT],
|
value_fn=lambda channel: channel[ATTR_SUBSCRIBER_COUNT],
|
||||||
entity_picture_fn=lambda channel: channel[ATTR_ICON],
|
entity_picture_fn=lambda channel: channel[ATTR_ICON],
|
||||||
|
attributes_fn=None,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -84,3 +90,10 @@ class YouTubeSensor(YouTubeChannelEntity, SensorEntity):
|
||||||
def entity_picture(self) -> str:
|
def entity_picture(self) -> str:
|
||||||
"""Return the value reported by the sensor."""
|
"""Return the value reported by the sensor."""
|
||||||
return self.entity_description.entity_picture_fn(self._channel)
|
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
|
||||||
|
|
|
@ -25,6 +25,7 @@ async def test_sensor(hass: HomeAssistant, setup_integration: ComponentSetup) ->
|
||||||
state.attributes["entity_picture"]
|
state.attributes["entity_picture"]
|
||||||
== "https://i.ytimg.com/vi/wysukDrMdqU/sddefault.jpg"
|
== "https://i.ytimg.com/vi/wysukDrMdqU/sddefault.jpg"
|
||||||
)
|
)
|
||||||
|
assert state.attributes["video_id"] == "wysukDrMdqU"
|
||||||
|
|
||||||
state = hass.states.get("sensor.google_for_developers_subscribers")
|
state = hass.states.get("sensor.google_for_developers_subscribers")
|
||||||
assert state
|
assert state
|
||||||
|
|
Loading…
Reference in New Issue