Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Contains sensors exposed by the Starlink integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import datetime, timedelta
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  DEGREE,
18  PERCENTAGE,
19  EntityCategory,
20  UnitOfDataRate,
21  UnitOfTime,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.typing import StateType
26 from homeassistant.util.dt import now
27 
28 from .const import DOMAIN
29 from .coordinator import StarlinkData
30 from .entity import StarlinkEntity
31 
32 
34  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
35 ) -> None:
36  """Set up all sensors for this entry."""
37  coordinator = hass.data[DOMAIN][entry.entry_id]
38 
40  StarlinkSensorEntity(coordinator, description) for description in SENSORS
41  )
42 
43 
44 @dataclass(frozen=True, kw_only=True)
46  """Describes a Starlink sensor entity."""
47 
48  value_fn: Callable[[StarlinkData], datetime | StateType]
49 
50 
52  """A SensorEntity for Starlink devices. Handles creating unique IDs."""
53 
54  entity_description: StarlinkSensorEntityDescription
55 
56  @property
57  def native_value(self) -> StateType | datetime:
58  """Calculate the sensor value from the entity description."""
59  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
60 
61 
62 SENSORS: tuple[StarlinkSensorEntityDescription, ...] = (
64  key="ping",
65  translation_key="ping",
66  state_class=SensorStateClass.MEASUREMENT,
67  native_unit_of_measurement=UnitOfTime.MILLISECONDS,
68  suggested_display_precision=0,
69  value_fn=lambda data: data.status["pop_ping_latency_ms"],
70  ),
72  key="azimuth",
73  translation_key="azimuth",
74  state_class=SensorStateClass.MEASUREMENT,
75  entity_category=EntityCategory.DIAGNOSTIC,
76  native_unit_of_measurement=DEGREE,
77  entity_registry_enabled_default=False,
78  suggested_display_precision=0,
79  value_fn=lambda data: data.status["direction_azimuth"],
80  ),
82  key="elevation",
83  translation_key="elevation",
84  state_class=SensorStateClass.MEASUREMENT,
85  entity_category=EntityCategory.DIAGNOSTIC,
86  native_unit_of_measurement=DEGREE,
87  entity_registry_enabled_default=False,
88  suggested_display_precision=0,
89  value_fn=lambda data: data.status["direction_elevation"],
90  ),
92  key="uplink_throughput",
93  translation_key="uplink_throughput",
94  state_class=SensorStateClass.MEASUREMENT,
95  device_class=SensorDeviceClass.DATA_RATE,
96  native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
97  suggested_display_precision=0,
98  value_fn=lambda data: data.status["uplink_throughput_bps"],
99  ),
101  key="downlink_throughput",
102  translation_key="downlink_throughput",
103  state_class=SensorStateClass.MEASUREMENT,
104  device_class=SensorDeviceClass.DATA_RATE,
105  native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
106  suggested_display_precision=0,
107  value_fn=lambda data: data.status["downlink_throughput_bps"],
108  ),
110  key="last_boot_time",
111  translation_key="last_boot_time",
112  device_class=SensorDeviceClass.TIMESTAMP,
113  entity_category=EntityCategory.DIAGNOSTIC,
114  value_fn=lambda data: now() - timedelta(seconds=data.status["uptime"]),
115  ),
117  key="ping_drop_rate",
118  translation_key="ping_drop_rate",
119  state_class=SensorStateClass.MEASUREMENT,
120  native_unit_of_measurement=PERCENTAGE,
121  value_fn=lambda data: data.status["pop_ping_drop_rate"] * 100,
122  ),
123 )
datetime now(HomeAssistant hass)
Definition: template.py:1890