Skip to content

Commit 6011df8

Browse files
authored
Refactor Starlink sensor construction (home-assistant#155591)
Signed-off-by: David Rapan <[email protected]>
1 parent 08e494a commit 6011df8

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

homeassistant/components/starlink/sensor.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,17 @@ async def async_setup_entry(
4040
) -> None:
4141
"""Set up all sensors for this entry."""
4242
async_add_entities(
43-
StarlinkSensorEntity(config_entry.runtime_data, description)
43+
description.entity_class(config_entry.runtime_data, description)
4444
for description in SENSORS
4545
)
4646

47-
async_add_entities(
48-
StarlinkRestoreSensor(config_entry.runtime_data, description)
49-
for description in RESTORE_SENSORS
50-
)
51-
5247

5348
@dataclass(frozen=True, kw_only=True)
5449
class StarlinkSensorEntityDescription(SensorEntityDescription):
5550
"""Describes a Starlink sensor entity."""
5651

5752
value_fn: Callable[[StarlinkData], datetime | StateType]
53+
entity_class: Callable
5854

5955

6056
class StarlinkSensorEntity(StarlinkEntity, SensorEntity):
@@ -68,14 +64,14 @@ def native_value(self) -> StateType | datetime:
6864
return self.entity_description.value_fn(self.coordinator.data)
6965

7066

71-
class StarlinkRestoreSensor(StarlinkSensorEntity, RestoreSensor):
72-
"""A RestoreSensorEntity for Starlink devices. Handles creating unique IDs."""
67+
class StarlinkAccumulationSensor(StarlinkSensorEntity, RestoreSensor):
68+
"""A StarlinkAccumulationSensor for Starlink devices. Handles creating unique IDs."""
7369

7470
_attr_native_value: int | float = 0
7571

7672
@property
7773
def native_value(self) -> int | float:
78-
"""Calculate the sensor value from current value and the entity description."""
74+
"""Calculate new value from current value and the entity description."""
7975
new_value = super().native_value
8076
if TYPE_CHECKING:
8177
assert isinstance(new_value, (int, float))
@@ -103,6 +99,7 @@ async def async_added_to_hass(self) -> None:
10399
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
104100
suggested_display_precision=0,
105101
value_fn=lambda data: data.status["pop_ping_latency_ms"],
102+
entity_class=StarlinkSensorEntity,
106103
),
107104
StarlinkSensorEntityDescription(
108105
key="azimuth",
@@ -113,6 +110,7 @@ async def async_added_to_hass(self) -> None:
113110
entity_registry_enabled_default=False,
114111
suggested_display_precision=0,
115112
value_fn=lambda data: data.status["direction_azimuth"],
113+
entity_class=StarlinkSensorEntity,
116114
),
117115
StarlinkSensorEntityDescription(
118116
key="elevation",
@@ -123,6 +121,7 @@ async def async_added_to_hass(self) -> None:
123121
entity_registry_enabled_default=False,
124122
suggested_display_precision=0,
125123
value_fn=lambda data: data.status["direction_elevation"],
124+
entity_class=StarlinkSensorEntity,
126125
),
127126
StarlinkSensorEntityDescription(
128127
key="uplink_throughput",
@@ -133,6 +132,7 @@ async def async_added_to_hass(self) -> None:
133132
suggested_display_precision=1,
134133
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
135134
value_fn=lambda data: data.status["uplink_throughput_bps"],
135+
entity_class=StarlinkSensorEntity,
136136
),
137137
StarlinkSensorEntityDescription(
138138
key="downlink_throughput",
@@ -143,6 +143,7 @@ async def async_added_to_hass(self) -> None:
143143
suggested_display_precision=1,
144144
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
145145
value_fn=lambda data: data.status["downlink_throughput_bps"],
146+
entity_class=StarlinkSensorEntity,
146147
),
147148
StarlinkSensorEntityDescription(
148149
key="last_boot_time",
@@ -152,13 +153,15 @@ async def async_added_to_hass(self) -> None:
152153
value_fn=lambda data: (
153154
now() - timedelta(seconds=data.status["uptime"], milliseconds=-500)
154155
).replace(microsecond=0),
156+
entity_class=StarlinkSensorEntity,
155157
),
156158
StarlinkSensorEntityDescription(
157159
key="ping_drop_rate",
158160
translation_key="ping_drop_rate",
159161
state_class=SensorStateClass.MEASUREMENT,
160162
native_unit_of_measurement=PERCENTAGE,
161163
value_fn=lambda data: data.status["pop_ping_drop_rate"] * 100,
164+
entity_class=StarlinkSensorEntity,
162165
),
163166
StarlinkSensorEntityDescription(
164167
key="power",
@@ -167,16 +170,16 @@ async def async_added_to_hass(self) -> None:
167170
native_unit_of_measurement=UnitOfPower.WATT,
168171
suggested_display_precision=0,
169172
value_fn=lambda data: data.consumption["latest_power"],
173+
entity_class=StarlinkSensorEntity,
170174
),
171-
)
172-
RESTORE_SENSORS: tuple[StarlinkSensorEntityDescription, ...] = (
173175
StarlinkSensorEntityDescription(
174176
key="energy",
175177
device_class=SensorDeviceClass.ENERGY,
176178
state_class=SensorStateClass.TOTAL_INCREASING,
177179
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
178180
suggested_display_precision=1,
179181
value_fn=lambda data: data.consumption["total_energy"],
182+
entity_class=StarlinkAccumulationSensor,
180183
),
181184
StarlinkSensorEntityDescription(
182185
key="download",
@@ -187,6 +190,7 @@ async def async_added_to_hass(self) -> None:
187190
suggested_display_precision=1,
188191
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
189192
value_fn=lambda data: data.usage["download_usage"],
193+
entity_class=StarlinkAccumulationSensor,
190194
),
191195
StarlinkSensorEntityDescription(
192196
key="upload",
@@ -197,5 +201,6 @@ async def async_added_to_hass(self) -> None:
197201
suggested_display_precision=1,
198202
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
199203
value_fn=lambda data: data.usage["upload_usage"],
204+
entity_class=StarlinkAccumulationSensor,
200205
),
201206
)

0 commit comments

Comments
 (0)