Skip to content

Commit ce14544

Browse files
authored
Add packet loss sensor to Ping integration (home-assistant#158081)
1 parent 87b9c31 commit ce14544

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

homeassistant/components/ping/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ async def async_update(self) -> None:
6464
return
6565

6666
_LOGGER.debug(
67-
"async_ping returned: reachable=%s sent=%i received=%s",
67+
"async_ping returned: reachable=%s sent=%i received=%s loss=%s",
6868
data.is_alive,
6969
data.packets_sent,
7070
data.packets_received,
71+
data.packet_loss * 100,
7172
)
7273

7374
self.is_alive = data.is_alive
@@ -80,6 +81,7 @@ async def async_update(self) -> None:
8081
"max": data.max_rtt,
8182
"avg": data.avg_rtt,
8283
"jitter": data.jitter,
84+
"loss": data.packet_loss * 100,
8385
}
8486

8587

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"entity": {
3+
"sensor": {
4+
"loss": {
5+
"default": "mdi:alert-circle-outline"
6+
}
7+
}
8+
}
9+
}

homeassistant/components/ping/sensor.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
SensorStateClass,
1111
)
1212
from homeassistant.config_entries import ConfigEntry
13-
from homeassistant.const import EntityCategory, UnitOfTime
13+
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime
1414
from homeassistant.core import HomeAssistant
1515
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1616

@@ -82,6 +82,16 @@ class PingSensorEntityDescription(SensorEntityDescription):
8282
value_fn=lambda result: result.data.get("jitter"),
8383
has_fn=lambda result: "jitter" in result.data,
8484
),
85+
PingSensorEntityDescription(
86+
key="loss",
87+
translation_key="loss",
88+
native_unit_of_measurement=PERCENTAGE,
89+
state_class=SensorStateClass.MEASUREMENT,
90+
entity_registry_enabled_default=False,
91+
entity_category=EntityCategory.DIAGNOSTIC,
92+
value_fn=lambda result: result.data.get("loss"),
93+
has_fn=lambda result: "loss" in result.data,
94+
),
8595
)
8696

8797

homeassistant/components/ping/strings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"jitter": {
2323
"name": "Jitter"
2424
},
25+
"loss": {
26+
"name": "Packet loss"
27+
},
2528
"round_trip_time_avg": {
2629
"name": "Round-trip time average"
2730
},

tests/components/ping/snapshots/test_sensor.ambr

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,57 @@
5454
'state': '3.5',
5555
})
5656
# ---
57+
# name: test_setup_and_update[packet_loss]
58+
EntityRegistryEntrySnapshot({
59+
'aliases': set({
60+
}),
61+
'area_id': None,
62+
'capabilities': dict({
63+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
64+
}),
65+
'config_entry_id': <ANY>,
66+
'config_subentry_id': <ANY>,
67+
'device_class': None,
68+
'device_id': <ANY>,
69+
'disabled_by': None,
70+
'domain': 'sensor',
71+
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
72+
'entity_id': 'sensor.10_10_10_10_packet_loss',
73+
'has_entity_name': True,
74+
'hidden_by': None,
75+
'icon': None,
76+
'id': <ANY>,
77+
'labels': set({
78+
}),
79+
'name': None,
80+
'options': dict({
81+
}),
82+
'original_device_class': None,
83+
'original_icon': None,
84+
'original_name': 'Packet loss',
85+
'platform': 'ping',
86+
'previous_unique_id': None,
87+
'suggested_object_id': None,
88+
'supported_features': 0,
89+
'translation_key': 'loss',
90+
'unit_of_measurement': '%',
91+
})
92+
# ---
93+
# name: test_setup_and_update[packet_loss].1
94+
StateSnapshot({
95+
'attributes': ReadOnlyDict({
96+
'friendly_name': '10.10.10.10 Packet loss',
97+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
98+
'unit_of_measurement': '%',
99+
}),
100+
'context': <ANY>,
101+
'entity_id': 'sensor.10_10_10_10_packet_loss',
102+
'last_changed': <ANY>,
103+
'last_reported': <ANY>,
104+
'last_updated': <ANY>,
105+
'state': '0.0',
106+
})
107+
# ---
57108
# name: test_setup_and_update[round_trip_time_average]
58109
EntityRegistryEntrySnapshot({
59110
'aliases': set({

tests/components/ping/test_sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"round_trip_time_mean_deviation", # should be None in the snapshot
1818
"round_trip_time_minimum",
1919
"jitter",
20+
"packet_loss",
2021
],
2122
)
2223
async def test_setup_and_update(

0 commit comments

Comments
 (0)