Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for the Twitch stream status."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.sensor import SensorEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 from homeassistant.helpers.typing import StateType
12 from homeassistant.helpers.update_coordinator import CoordinatorEntity
13 
14 from . import TwitchCoordinator
15 from .const import DOMAIN
16 from .coordinator import TwitchUpdate
17 
18 ATTR_GAME = "game"
19 ATTR_TITLE = "title"
20 ATTR_SUBSCRIPTION = "subscribed"
21 ATTR_SUBSCRIPTION_GIFTED = "subscription_is_gifted"
22 ATTR_SUBSCRIPTION_TIER = "subscription_tier"
23 ATTR_FOLLOW = "following"
24 ATTR_FOLLOW_SINCE = "following_since"
25 ATTR_FOLLOWING = "followers"
26 ATTR_VIEWERS = "viewers"
27 ATTR_STARTED_AT = "started_at"
28 
29 STATE_OFFLINE = "offline"
30 STATE_STREAMING = "streaming"
31 
32 PARALLEL_UPDATES = 1
33 
34 
36  hass: HomeAssistant,
37  entry: ConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Initialize entries."""
41  coordinator = hass.data[DOMAIN][entry.entry_id]
42 
44  TwitchSensor(coordinator, channel_id) for channel_id in coordinator.data
45  )
46 
47 
48 class TwitchSensor(CoordinatorEntity[TwitchCoordinator], SensorEntity):
49  """Representation of a Twitch channel."""
50 
51  _attr_translation_key = "channel"
52 
53  def __init__(self, coordinator: TwitchCoordinator, channel_id: str) -> None:
54  """Initialize the sensor."""
55  super().__init__(coordinator)
56  self.channel_idchannel_id = channel_id
57  self._attr_unique_id_attr_unique_id = channel_id
58  self._attr_name_attr_name = self.channelchannel.name
59 
60  @property
61  def available(self) -> bool:
62  """Return if entity is available."""
63  return super().available and self.channel_idchannel_id in self.coordinator.data
64 
65  @property
66  def channel(self) -> TwitchUpdate:
67  """Return the channel data."""
68  return self.coordinator.data[self.channel_idchannel_id]
69 
70  @property
71  def native_value(self) -> StateType:
72  """Return the state of the sensor."""
73  return STATE_STREAMING if self.channelchannel.is_streaming else STATE_OFFLINE
74 
75  @property
76  def extra_state_attributes(self) -> dict[str, Any]:
77  """Return the state attributes."""
78  channel = self.channelchannel
79  resp = {
80  ATTR_FOLLOWING: channel.followers,
81  ATTR_GAME: channel.game,
82  ATTR_TITLE: channel.title,
83  ATTR_STARTED_AT: channel.started_at,
84  ATTR_VIEWERS: channel.viewers,
85  }
86  resp[ATTR_SUBSCRIPTION] = False
87  if channel.subscribed is not None:
88  resp[ATTR_SUBSCRIPTION] = channel.subscribed
89  resp[ATTR_SUBSCRIPTION_GIFTED] = channel.subscription_gifted
90  resp[ATTR_SUBSCRIPTION_TIER] = channel.subscription_tier
91  resp[ATTR_FOLLOW] = channel.follows
92  if channel.follows:
93  resp[ATTR_FOLLOW_SINCE] = channel.following_since
94  return resp
95 
96  @property
97  def entity_picture(self) -> str | None:
98  """Return the picture of the sensor."""
99  if self.channelchannel.is_streaming:
100  assert self.channelchannel.stream_picture is not None
101  return self.channelchannel.stream_picture
102  return self.channelchannel.picture
None __init__(self, TwitchCoordinator coordinator, str channel_id)
Definition: sensor.py:53
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:39