-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathsensor.py
158 lines (142 loc) · 5.61 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""Contains sensors exposed by the Starlink integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timedelta
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
DEGREE,
PERCENTAGE,
EntityCategory,
UnitOfDataRate,
UnitOfEnergy,
UnitOfInformation,
UnitOfPower,
UnitOfTime,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import now
from .const import DOMAIN
from .coordinator import StarlinkData
from .entity import StarlinkEntity
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up all sensors for this entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
StarlinkSensorEntity(coordinator, description) for description in SENSORS
)
@dataclass(frozen=True, kw_only=True)
class StarlinkSensorEntityDescription(SensorEntityDescription):
"""Describes a Starlink sensor entity."""
value_fn: Callable[[StarlinkData], datetime | StateType]
class StarlinkSensorEntity(StarlinkEntity, SensorEntity):
"""A SensorEntity for Starlink devices. Handles creating unique IDs."""
entity_description: StarlinkSensorEntityDescription
@property
def native_value(self) -> StateType | datetime:
"""Calculate the sensor value from the entity description."""
return self.entity_description.value_fn(self.coordinator.data)
SENSORS: tuple[StarlinkSensorEntityDescription, ...] = (
StarlinkSensorEntityDescription(
key="ping",
translation_key="ping",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
suggested_display_precision=0,
value_fn=lambda data: data.status["pop_ping_latency_ms"],
),
StarlinkSensorEntityDescription(
key="azimuth",
translation_key="azimuth",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DEGREE,
entity_registry_enabled_default=False,
suggested_display_precision=0,
value_fn=lambda data: data.status["direction_azimuth"],
),
StarlinkSensorEntityDescription(
key="elevation",
translation_key="elevation",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DEGREE,
entity_registry_enabled_default=False,
suggested_display_precision=0,
value_fn=lambda data: data.status["direction_elevation"],
),
StarlinkSensorEntityDescription(
key="uplink_throughput",
translation_key="uplink_throughput",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
suggested_display_precision=0,
value_fn=lambda data: data.status["uplink_throughput_bps"],
),
StarlinkSensorEntityDescription(
key="downlink_throughput",
translation_key="downlink_throughput",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
suggested_display_precision=0,
value_fn=lambda data: data.status["downlink_throughput_bps"],
),
StarlinkSensorEntityDescription(
key="last_boot_time",
translation_key="last_boot_time",
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: now() - timedelta(seconds=data.status["uptime"]),
),
StarlinkSensorEntityDescription(
key="ping_drop_rate",
translation_key="ping_drop_rate",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
value_fn=lambda data: data.status["pop_ping_drop_rate"] * 100,
),
StarlinkSensorEntityDescription(
key="upload",
translation_key="upload",
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
value_fn=lambda data: data.usage["upload_usage"],
),
StarlinkSensorEntityDescription(
key="download",
translation_key="download",
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
value_fn=lambda data: data.usage["download_usage"],
),
StarlinkSensorEntityDescription(
key="power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
value_fn=lambda data: data.consumption["latest_power"],
),
StarlinkSensorEntityDescription(
key="energy",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
value_fn=lambda data: data.consumption["total_energy"],
),
)