1 """Support for YouTube Sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
16 from .
import YouTubeDataUpdateCoordinator
20 ATTR_SUBSCRIBER_COUNT,
28 from .entity
import YouTubeChannelEntity
31 @dataclass(frozen=True, kw_only=True)
33 """Describes YouTube sensor entity."""
35 available_fn: Callable[[Any], bool]
36 value_fn: Callable[[Any], StateType]
37 entity_picture_fn: Callable[[Any], str |
None]
38 attributes_fn: Callable[[Any], dict[str, Any] |
None] |
None
44 translation_key=
"latest_upload",
45 available_fn=
lambda channel: channel[ATTR_LATEST_VIDEO]
is not None,
46 value_fn=
lambda channel: channel[ATTR_LATEST_VIDEO][ATTR_TITLE],
47 entity_picture_fn=
lambda channel: channel[ATTR_LATEST_VIDEO][ATTR_THUMBNAIL],
48 attributes_fn=
lambda channel: {
49 ATTR_VIDEO_ID: channel[ATTR_LATEST_VIDEO][ATTR_VIDEO_ID],
50 ATTR_PUBLISHED_AT: channel[ATTR_LATEST_VIDEO][ATTR_PUBLISHED_AT],
55 translation_key=
"subscribers",
56 native_unit_of_measurement=
"subscribers",
57 available_fn=
lambda _:
True,
58 value_fn=
lambda channel: channel[ATTR_SUBSCRIBER_COUNT],
59 entity_picture_fn=
lambda channel: channel[ATTR_ICON],
64 translation_key=
"views",
65 native_unit_of_measurement=
"views",
66 available_fn=
lambda _:
True,
67 value_fn=
lambda channel: channel[ATTR_TOTAL_VIEWS],
68 entity_picture_fn=
lambda channel: channel[ATTR_ICON],
75 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
77 """Set up the YouTube sensor."""
78 coordinator: YouTubeDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
83 for channel_id
in coordinator.data
84 for sensor_type
in SENSOR_TYPES
89 """Representation of a YouTube sensor."""
91 entity_description: YouTubeSensorEntityDescription
95 """Return if the entity is available."""
102 """Return the value reported by the sensor."""
107 """Return the value reported by the sensor."""
116 """Return the extra state attributes."""
StateType native_value(self)
str|None entity_picture(self)
dict[str, Any]|None extra_state_attributes(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)