Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for YouTube Sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import ATTR_ICON
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.typing import StateType
15 
16 from . import YouTubeDataUpdateCoordinator
17 from .const import (
18  ATTR_LATEST_VIDEO,
19  ATTR_PUBLISHED_AT,
20  ATTR_SUBSCRIBER_COUNT,
21  ATTR_THUMBNAIL,
22  ATTR_TITLE,
23  ATTR_TOTAL_VIEWS,
24  ATTR_VIDEO_ID,
25  COORDINATOR,
26  DOMAIN,
27 )
28 from .entity import YouTubeChannelEntity
29 
30 
31 @dataclass(frozen=True, kw_only=True)
33  """Describes YouTube sensor entity."""
34 
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
39 
40 
41 SENSOR_TYPES = [
43  key="latest_upload",
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],
51  },
52  ),
54  key="subscribers",
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],
60  attributes_fn=None,
61  ),
63  key="views",
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],
69  attributes_fn=None,
70  ),
71 ]
72 
73 
75  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
76 ) -> None:
77  """Set up the YouTube sensor."""
78  coordinator: YouTubeDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
79  COORDINATOR
80  ]
82  YouTubeSensor(coordinator, sensor_type, channel_id)
83  for channel_id in coordinator.data
84  for sensor_type in SENSOR_TYPES
85  )
86 
87 
89  """Representation of a YouTube sensor."""
90 
91  entity_description: YouTubeSensorEntityDescription
92 
93  @property
94  def available(self) -> bool:
95  """Return if the entity is available."""
96  return super().available and self.entity_descriptionentity_description.available_fn(
97  self.coordinator.data[self._channel_id_channel_id]
98  )
99 
100  @property
101  def native_value(self) -> StateType:
102  """Return the value reported by the sensor."""
103  return self.entity_descriptionentity_description.value_fn(self.coordinator.data[self._channel_id_channel_id])
104 
105  @property
106  def entity_picture(self) -> str | None:
107  """Return the value reported by the sensor."""
108  if not self.availableavailableavailableavailable:
109  return None
110  return self.entity_descriptionentity_description.entity_picture_fn(
111  self.coordinator.data[self._channel_id_channel_id]
112  )
113 
114  @property
115  def extra_state_attributes(self) -> dict[str, Any] | None:
116  """Return the extra state attributes."""
117  if self.entity_descriptionentity_description.attributes_fn:
118  return self.entity_descriptionentity_description.attributes_fn(
119  self.coordinator.data[self._channel_id_channel_id]
120  )
121  return None
dict[str, Any]|None extra_state_attributes(self)
Definition: sensor.py:115
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:76