Skip to content

Commit

Permalink
Add fiber RX TX sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius committed May 8, 2024
1 parent 5c2559e commit d45d032
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 11 deletions.
29 changes: 22 additions & 7 deletions custom_components/livebox/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ async def _async_update_data(self) -> dict[str, Any]:
},
"ddns": await self.async_get_ddns(),
"wifi_stats": await self.async_get_wifi_stats(),
"fiber_status": await self.async_get_fiber_status(),
"fiber_stats": await self.async_get_fiber_stats(),
}
except AiosysbusException as error:
_LOGGER.error("Error while fetch data information: %s", error)
Expand Down Expand Up @@ -130,10 +132,28 @@ async def async_get_caller_missed(self) -> list[dict[str, Any] | None]:
async def async_get_dsl_status(self) -> dict[str, Any]:
"""Get dsl status."""
parameters = {"mibs": "dsl", "flag": "", "traverse": "down"}
dsl_status = await self._make_request(
dsl0 = await self._make_request(
self.api.nemo.async_get_MIBs, "data", parameters
)
return dsl_status.get("status", {}).get("dsl", {}).get("dsl0", {})
return dsl0.get("status", {}).get("dsl", {}).get("dsl0", {})

async def async_get_fiber_status(self):
"""Get fiber status."""
parameters = {"mibs": "gpon"}
veip0 = await self._make_request(
self.api.nemo.async_get_MIBs, "veip0", parameters
)
return veip0.get("status", {}).get("gpon", {}).get("veip0", {})

async def async_get_wifi_stats(self) -> bool:
"""Get wifi stats."""
stats = await self._make_request(self.api.nmc.async_get_wifi_stats)
return stats.get("data", {})

async def async_get_fiber_stats(self) -> bool:
"""Get fiber stats."""
stats = await self._make_request(self.api.nemo.async_get_net_dev_stats, "veip0")
return stats.get("status", {})

async def async_get_wan_status(self) -> dict[str, Any]:
"""Get status."""
Expand All @@ -150,11 +170,6 @@ async def async_get_wifi(self) -> bool:
wifi = await self._make_request(self.api.nmc.async_get_wifi)
return wifi.get("status", {}).get("Enable") is True

async def async_get_wifi_stats(self) -> bool:
"""Get wifi stats."""
stats = await self._make_request(self.api.nmc.async_get_wifi_stats)
return stats.get("data", {})

async def async_get_guest_wifi(self) -> bool:
"""Get Guest Wifi status."""
guest_wifi = await self._make_request(self.api.nmc.async_get_guest_wifi)
Expand Down
10 changes: 7 additions & 3 deletions custom_components/livebox/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ async def async_get_config_entry_diagnostics(
(coordinator.api.nemo.async_lucky_addr_address, ["data"]),
(coordinator.api.nemo.async_get_MIBs, ["data"]),
(coordinator.api.nemo.async_get_MIBs, ["lan"]),
(coordinator.api.nemo.async_get_net_dev_stats, ["data"]),
(coordinator.api.nemo.async_get_MIBs, ["veip0"]),
(coordinator.api.nemo.async_get_net_dev_stats, ["eth0"]),
(coordinator.api.nemo.async_get_net_dev_stats, ["veip0"]),
coordinator.api.nemo.async_get_dsl0_line_stats,
coordinator.api.sfp.async_get,
coordinator.api.schedule.async_get_scheduletypes,
coordinator.api.dhcp.async_get_dhcp_pool,
coordinator.api.dhcp.async_get_dhcp_stats,
Expand All @@ -135,11 +138,12 @@ async def async_get_config_entry_diagnostics(
coordinator.api.userinterface.async_get_language,
coordinator.api.userinterface.async_get_state,
coordinator.api.upnpigd.async_get,
# coordinator.api.homelan.async_get_lan, # take 5s
# coordinator.api.homelan.async_get_results, # take 5s
# coordinator.api.homelan.async_get_devices_results, # take 13s
coordinator.api.homelan.async_get_maxnumber_records,
coordinator.api.homelan.async_get_reading_interval,
coordinator.api.homelan.async_get_devices_reading_interval,
coordinator.api.homelan.async_get_devices_status,
# coordinator.api.homelan.async_get_devices_results, # take 13s
coordinator.api.screen.async_get_show_wifi_password,
coordinator.api.pnp.async_get,
coordinator.api.iotservice.async_get_status,
Expand Down
77 changes: 76 additions & 1 deletion custom_components/livebox/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfDataRate, UnitOfInformation
from homeassistant.const import (
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
UnitOfDataRate,
UnitOfInformation,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand Down Expand Up @@ -102,6 +106,77 @@ class LiveboxSensorEntityDescription(SensorEntityDescription):
translation_key="wifi_tx",
entity_registry_enabled_default=False,
),
LiveboxSensorEntityDescription(
key="fiber_power_rx",
name="Fiber Power Rx",
value_fn=lambda x: x.get("fiber_status", {}).get("SignalRxPower"),
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
state_class=SensorStateClass.MEASUREMENT,
translation_key="fiber_power_rx",
attrs={
"Downstream max rate": lambda x: x.get("fiber_status", {}).get(
"DownstreamMaxRate"
),
"Downstream current rate": lambda x: x.get("fiber_status", {}).get(
"DownstreamCurrRate"
),
"Max bitrate (gbps)": lambda x: x.get("fiber_status", {}).get(
"MaxBitRateSupported", 0
)
/ 1000,
"Temperature": lambda x: x.get("fiber_status", {}).get("Temperature"),
"Voltage": lambda x: x.get("fiber_status", {}).get("Voltage"),
"Bias": lambda x: x.get("fiber_status", {}).get("Bias"),
"ONU State": lambda x: x.get("fiber_status", {}).get("ONUState"),
},
),
LiveboxSensorEntityDescription(
key="fiber_power_tx",
name="Fiber Power Tx",
value_fn=lambda x: x.get("fiber_status", {}).get("SignalTxPower", 0),
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
state_class=SensorStateClass.MEASUREMENT,
translation_key="fiber_power_tx",
attrs={
"Upstream max rate": lambda x: x.get("fiber_status", {}).get(
"UpstreamMaxRate"
),
"Upstream current rate (mb)": lambda x: x.get("fiber_status", {}).get(
"UpstreamCurrRate"
),
"Max bitrate (gbps)": lambda x: x.get("fiber_status", {}).get(
"MaxBitRateSupported", 0
)
/ 1000,
"Tx power (dbm)": lambda x: x.get("fiber_status", {}).get("SignalTxPower"),
"Temperature": lambda x: x.get("fiber_status", {}).get("Temperature"),
"Voltage": lambda x: x.get("fiber_status", {}).get("Voltage"),
"Bias": lambda x: x.get("fiber_status", {}).get("Bias"),
"ONU State": lambda x: x.get("fiber_status", {}).get("ONUState"),
},
),
LiveboxSensorEntityDescription(
key="fiber_tx",
name="Fiber Tx",
value_fn=lambda x: round(
x.get("fiber_stats", {}).get("TxBytes", 0) / 1048576, 2
),
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
translation_key="fiber_tx",
attrs={"Tx errors": lambda x: x.get("fiber_stats", {}).get("TxErrors")},
),
LiveboxSensorEntityDescription(
key="fiber_rx",
name="Fiber Rx",
value_fn=lambda x: round(
x.get("fiber_stats", {}).get("RxBytes", 0) / 1048576, 2
),
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
translation_key="fiber_rx",
attrs={"Tx errors": lambda x: x.get("fiber_stats", {}).get("RxErrors")},
),
)


Expand Down

0 comments on commit d45d032

Please sign in to comment.