Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """The FiveM sensor platform."""
2 
3 from dataclasses import dataclass
4 
5 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 from homeassistant.helpers.typing import StateType
10 
11 from .const import (
12  ATTR_PLAYERS_LIST,
13  ATTR_RESOURCES_LIST,
14  DOMAIN,
15  NAME_PLAYERS_MAX,
16  NAME_PLAYERS_ONLINE,
17  NAME_RESOURCES,
18  UNIT_PLAYERS_MAX,
19  UNIT_PLAYERS_ONLINE,
20  UNIT_RESOURCES,
21 )
22 from .entity import FiveMEntity, FiveMEntityDescription
23 
24 
25 @dataclass(frozen=True)
27  """Describes FiveM sensor entity."""
28 
29 
30 SENSORS: tuple[FiveMSensorEntityDescription, ...] = (
32  key=NAME_PLAYERS_MAX,
33  translation_key="max_players",
34  native_unit_of_measurement=UNIT_PLAYERS_MAX,
35  ),
37  key=NAME_PLAYERS_ONLINE,
38  translation_key="online_players",
39  native_unit_of_measurement=UNIT_PLAYERS_ONLINE,
40  extra_attrs=[ATTR_PLAYERS_LIST],
41  ),
43  key=NAME_RESOURCES,
44  translation_key="resources",
45  native_unit_of_measurement=UNIT_RESOURCES,
46  extra_attrs=[ATTR_RESOURCES_LIST],
47  ),
48 )
49 
50 
52  hass: HomeAssistant,
53  entry: ConfigEntry,
54  async_add_entities: AddEntitiesCallback,
55 ) -> None:
56  """Set up the FiveM sensor platform."""
57  coordinator = hass.data[DOMAIN][entry.entry_id]
58 
59  # Add sensor entities.
61  [FiveMSensorEntity(coordinator, description) for description in SENSORS]
62  )
63 
64 
66  """Representation of a FiveM sensor base entity."""
67 
68  entity_description: FiveMSensorEntityDescription
69 
70  @property
71  def native_value(self) -> StateType:
72  """Return the state of the sensor."""
73  return self.coordinator.data[self.entity_descriptionentity_description.key]
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:55