-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathsensor.py
117 lines (99 loc) · 4 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
"""Sensor platform that for Ping integration."""
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import PingConfigEntry
from .coordinator import PingResult, PingUpdateCoordinator
from .entity import PingEntity
@dataclass(frozen=True, kw_only=True)
class PingSensorEntityDescription(SensorEntityDescription):
"""Class to describe a Ping sensor entity."""
value_fn: Callable[[PingResult], float | None]
has_fn: Callable[[PingResult], bool]
SENSORS: tuple[PingSensorEntityDescription, ...] = (
PingSensorEntityDescription(
key="round_trip_time_avg",
translation_key="round_trip_time_avg",
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda result: result.data.get("avg"),
has_fn=lambda result: "avg" in result.data,
),
PingSensorEntityDescription(
key="round_trip_time_max",
translation_key="round_trip_time_max",
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda result: result.data.get("max"),
has_fn=lambda result: "max" in result.data,
),
PingSensorEntityDescription(
key="round_trip_time_mdev",
translation_key="round_trip_time_mdev",
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda result: result.data.get("mdev"),
has_fn=lambda result: "mdev" in result.data,
),
PingSensorEntityDescription(
key="round_trip_time_min",
translation_key="round_trip_time_min",
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda result: result.data.get("min"),
has_fn=lambda result: "min" in result.data,
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: PingConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Ping sensors from config entry."""
coordinator = entry.runtime_data
async_add_entities(
PingSensor(entry, description, coordinator)
for description in SENSORS
if description.has_fn(coordinator.data)
)
class PingSensor(PingEntity, SensorEntity):
"""Represents a Ping sensor."""
entity_description: PingSensorEntityDescription
def __init__(
self,
config_entry: ConfigEntry,
description: PingSensorEntityDescription,
coordinator: PingUpdateCoordinator,
) -> None:
"""Initialize the sensor."""
super().__init__(
config_entry, coordinator, f"{config_entry.entry_id}-{description.key}"
)
self.entity_description = description
@property
def available(self) -> bool:
"""Return True if entity is available."""
return super().available and self.coordinator.data.is_alive
@property
def native_value(self) -> float | None:
"""Return the sensor state."""
return self.entity_description.value_fn(self.coordinator.data)