Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
feat: add packets per second sensor (#18)
Browse files Browse the repository at this point in the history
* feat: add packets per second sensor
* Bump library to 0.4.3

---------

Co-authored-by: Simon Roberts <lyricnz@gmail.com>
  • Loading branch information
firstof9 and lyricnz committed Feb 1, 2023
1 parent d532c6b commit 27f890d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion custom_components/tplink_ess/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@firstof9"
],
"requirements": [
"tplink-ess-lib==0.4.2"
"tplink-ess-lib==0.4.3"
],
"loggers": [
"tplink-ess-lib"
Expand Down
54 changes: 45 additions & 9 deletions custom_components/tplink_ess/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback
name=f"{prefix} Port {name} TxGoodPkt",
key="TxGoodPkt",
icon="mdi:upload-network",
native_unit_of_measurement="packets",
entity_registry_enabled_default=False,
),
coordinator,
Expand All @@ -81,6 +82,7 @@ async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback
name=f"{prefix} Port {name} RxGoodPkt",
key="RxGoodPkt",
icon="mdi:download-network",
native_unit_of_measurement="packets",
entity_registry_enabled_default=False,
),
coordinator,
Expand All @@ -99,6 +101,34 @@ async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback
coordinator,
entry,
),
),
sensors.append(
TPLinkESSSensor(
TPLinkSensorEntityDescription(
port=i,
name=f"{prefix} Port {name} PPS RX",
key="RxGoodPkt",
icon="mdi:lan-pending",
native_unit_of_measurement="packets/s",
entity_registry_enabled_default=False,
),
coordinator,
entry,
),
),
sensors.append(
TPLinkESSSensor(
TPLinkSensorEntityDescription(
port=i,
name=f"{prefix} Port {name} PPS TX",
key="TxGoodPkt",
icon="mdi:lan-pending",
native_unit_of_measurement="packets/s",
entity_registry_enabled_default=False,
),
coordinator,
entry,
),
)
async_add_entities(sensors, False)

Expand All @@ -122,14 +152,8 @@ def __init__(
self._key = sensor_description.key
self._attr_name = sensor_description.name
self._attr_unique_id = f"{sensor_description.name}_{config.entry_id}"
self._last_reading = None

@property
def native_unit_of_measurement(self) -> Any:
"""Return the unit of measurement."""
if self._key in ("TxGoodPkt", "RxGoodPkt"):
return "packets"
return None
self._attr_native_unit_of_measurement = sensor_description.native_unit_of_measurement
self._last_reading = 0.0

@property
def native_value(self):
Expand All @@ -139,8 +163,20 @@ def native_value(self):
return data.get("vlan")["vlan"][self._item_id]["VLAN ID"]
if self._key == "pvid":
return data.get("pvid")["pvid"][self._item_id][1]
if (value := data.get("stats")["stats"][self._item_id].get(self._key)) is not None:
if (
value := data.get("stats")["stats"][self._item_id].get(self._key)
) is not None:
if self._key in ("TxGoodPkt", "RxGoodPkt"):
if self._attr_native_unit_of_measurement == "packets/s":
rate = 0.0
if self._last_reading:
rate = round(float(
(value - self._last_reading)
/ self.coordinator.update_interval.total_seconds()
),2)
self._last_reading = float(value)
self._last_reading = int(value)
return float(rate)
return int(value)
if self._last_reading != value:
self._last_reading = value
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tplink-ess-lib==0.4.2
tplink-ess-lib==0.4.3

0 comments on commit 27f890d

Please sign in to comment.