Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for NYT Games sensors."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 from datetime import date
6 
7 from nyt_games import Connections, SpellingBee, Wordle
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import UnitOfTime
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import StateType
19 
20 from . import NYTGamesConfigEntry
21 from .coordinator import NYTGamesCoordinator
22 from .entity import ConnectionsEntity, SpellingBeeEntity, WordleEntity
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Describes a NYT Games Wordle sensor entity."""
28 
29  value_fn: Callable[[Wordle], StateType]
30 
31 
32 WORDLE_SENSORS: tuple[NYTGamesWordleSensorEntityDescription, ...] = (
34  key="wordles_played",
35  translation_key="wordles_played",
36  state_class=SensorStateClass.MEASUREMENT,
37  native_unit_of_measurement="games",
38  value_fn=lambda wordle: wordle.games_played,
39  ),
41  key="wordles_won",
42  translation_key="won",
43  state_class=SensorStateClass.MEASUREMENT,
44  native_unit_of_measurement="games",
45  value_fn=lambda wordle: wordle.games_won,
46  ),
48  key="wordles_streak",
49  translation_key="streak",
50  state_class=SensorStateClass.TOTAL,
51  native_unit_of_measurement=UnitOfTime.DAYS,
52  device_class=SensorDeviceClass.DURATION,
53  value_fn=lambda wordle: wordle.current_streak,
54  ),
56  key="wordles_max_streak",
57  translation_key="max_streak",
58  state_class=SensorStateClass.TOTAL_INCREASING,
59  native_unit_of_measurement=UnitOfTime.DAYS,
60  device_class=SensorDeviceClass.DURATION,
61  value_fn=lambda wordle: wordle.max_streak,
62  ),
63 )
64 
65 
66 @dataclass(frozen=True, kw_only=True)
68  """Describes a NYT Games Spelling Bee sensor entity."""
69 
70  value_fn: Callable[[SpellingBee], StateType]
71 
72 
73 SPELLING_BEE_SENSORS: tuple[NYTGamesSpellingBeeSensorEntityDescription, ...] = (
75  key="spelling_bees_played",
76  translation_key="spelling_bees_played",
77  state_class=SensorStateClass.TOTAL,
78  native_unit_of_measurement="games",
79  value_fn=lambda spelling_bee: spelling_bee.puzzles_started,
80  ),
82  key="spelling_bees_total_words",
83  translation_key="total_words",
84  state_class=SensorStateClass.TOTAL,
85  native_unit_of_measurement="words",
86  entity_registry_enabled_default=False,
87  value_fn=lambda spelling_bee: spelling_bee.total_words,
88  ),
90  key="spelling_bees_total_pangrams",
91  translation_key="total_pangrams",
92  state_class=SensorStateClass.TOTAL,
93  native_unit_of_measurement="pangrams",
94  entity_registry_enabled_default=False,
95  value_fn=lambda spelling_bee: spelling_bee.total_pangrams,
96  ),
97 )
98 
99 
100 @dataclass(frozen=True, kw_only=True)
102  """Describes a NYT Games Connections sensor entity."""
103 
104  value_fn: Callable[[Connections], StateType | date]
105 
106 
107 CONNECTIONS_SENSORS: tuple[NYTGamesConnectionsSensorEntityDescription, ...] = (
109  key="connections_played",
110  translation_key="connections_played",
111  state_class=SensorStateClass.TOTAL,
112  native_unit_of_measurement="games",
113  value_fn=lambda connections: connections.puzzles_completed,
114  ),
116  key="connections_won",
117  translation_key="won",
118  state_class=SensorStateClass.TOTAL,
119  native_unit_of_measurement="games",
120  value_fn=lambda connections: connections.puzzles_won,
121  ),
123  key="connections_last_played",
124  translation_key="last_played",
125  device_class=SensorDeviceClass.DATE,
126  value_fn=lambda connections: connections.last_completed,
127  ),
129  key="connections_streak",
130  translation_key="streak",
131  state_class=SensorStateClass.TOTAL,
132  native_unit_of_measurement=UnitOfTime.DAYS,
133  device_class=SensorDeviceClass.DURATION,
134  value_fn=lambda connections: connections.current_streak,
135  ),
137  key="connections_max_streak",
138  translation_key="max_streak",
139  state_class=SensorStateClass.TOTAL_INCREASING,
140  native_unit_of_measurement=UnitOfTime.DAYS,
141  device_class=SensorDeviceClass.DURATION,
142  value_fn=lambda connections: connections.max_streak,
143  ),
144 )
145 
146 
148  hass: HomeAssistant,
149  entry: NYTGamesConfigEntry,
150  async_add_entities: AddEntitiesCallback,
151 ) -> None:
152  """Set up NYT Games sensor entities based on a config entry."""
153 
154  coordinator = entry.runtime_data
155 
156  entities: list[SensorEntity] = [
157  NYTGamesWordleSensor(coordinator, description) for description in WORDLE_SENSORS
158  ]
159  if coordinator.data.spelling_bee is not None:
160  entities.extend(
161  NYTGamesSpellingBeeSensor(coordinator, description)
162  for description in SPELLING_BEE_SENSORS
163  )
164  if coordinator.data.connections is not None:
165  entities.extend(
166  NYTGamesConnectionsSensor(coordinator, description)
167  for description in CONNECTIONS_SENSORS
168  )
169 
170  async_add_entities(entities)
171 
172 
174  """Defines a NYT Games sensor."""
175 
176  entity_description: NYTGamesWordleSensorEntityDescription
177 
178  def __init__(
179  self,
180  coordinator: NYTGamesCoordinator,
181  description: NYTGamesWordleSensorEntityDescription,
182  ) -> None:
183  """Initialize NYT Games sensor."""
184  super().__init__(coordinator)
185  self.entity_descriptionentity_description = description
186  self._attr_unique_id_attr_unique_id = (
187  f"{coordinator.config_entry.unique_id}-wordle-{description.key}"
188  )
189 
190  @property
191  def native_value(self) -> StateType:
192  """Return the state of the sensor."""
193  return self.entity_descriptionentity_description.value_fn(self.coordinator.data.wordle)
194 
195 
197  """Defines a NYT Games sensor."""
198 
199  entity_description: NYTGamesSpellingBeeSensorEntityDescription
200 
201  def __init__(
202  self,
203  coordinator: NYTGamesCoordinator,
204  description: NYTGamesSpellingBeeSensorEntityDescription,
205  ) -> None:
206  """Initialize NYT Games sensor."""
207  super().__init__(coordinator)
208  self.entity_descriptionentity_description = description
209  self._attr_unique_id_attr_unique_id = (
210  f"{coordinator.config_entry.unique_id}-spelling_bee-{description.key}"
211  )
212 
213  @property
214  def native_value(self) -> StateType:
215  """Return the state of the sensor."""
216  assert self.coordinator.data.spelling_bee is not None
217  return self.entity_descriptionentity_description.value_fn(self.coordinator.data.spelling_bee)
218 
219 
221  """Defines a NYT Games sensor."""
222 
223  entity_description: NYTGamesConnectionsSensorEntityDescription
224 
225  def __init__(
226  self,
227  coordinator: NYTGamesCoordinator,
228  description: NYTGamesConnectionsSensorEntityDescription,
229  ) -> None:
230  """Initialize NYT Games sensor."""
231  super().__init__(coordinator)
232  self.entity_descriptionentity_description = description
233  self._attr_unique_id_attr_unique_id = (
234  f"{coordinator.config_entry.unique_id}-connections-{description.key}"
235  )
236 
237  @property
238  def native_value(self) -> StateType | date:
239  """Return the state of the sensor."""
240  assert self.coordinator.data.connections is not None
241  return self.entity_descriptionentity_description.value_fn(self.coordinator.data.connections)
None __init__(self, NYTGamesCoordinator coordinator, NYTGamesConnectionsSensorEntityDescription description)
Definition: sensor.py:229
None __init__(self, NYTGamesCoordinator coordinator, NYTGamesSpellingBeeSensorEntityDescription description)
Definition: sensor.py:205
None __init__(self, NYTGamesCoordinator coordinator, NYTGamesWordleSensorEntityDescription description)
Definition: sensor.py:182
None async_setup_entry(HomeAssistant hass, NYTGamesConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:151