Skip to content

Commit

Permalink
Merge pull request #280 from Snuffy2/Add-additional-details-to-Interf…
Browse files Browse the repository at this point in the history
…aces

Add additional details to Interfaces
  • Loading branch information
alexdelprete authored Oct 21, 2024
2 parents ea35db0 + 93517f5 commit da891d5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
17 changes: 14 additions & 3 deletions custom_components/opnsense/pyopnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,15 +1106,26 @@ async def get_interfaces(self) -> Mapping[str, Any]:
interface["collisions"] = self._try_to_int(
ifinfo.get("statistics", {}).get("collisions", None)
)
interface["descr"] = ifinfo.get("identifier", "")
interface["interface"] = ifinfo.get("identifier", "")
interface["name"] = ifinfo.get("description", "")
interface["status"] = ""
if ifinfo.get("status", "") in ("down", "no carrier", "up"):
interface["status"] = ifinfo.get("status", "")
elif ifinfo.get("status", "") in ("associated"):
interface["status"] = "up"
interface["ipaddr"] = ifinfo.get("addr4", "")
interface["media"] = ifinfo.get("media", "")
interface["ipv4"] = ifinfo.get("addr4", None)
interface["ipv6"] = ifinfo.get("addr6", None)
interface["media"] = ifinfo.get("media", None)
interface["gateways"] = ifinfo.get("gateways", [])
interface["routes"] = ifinfo.get("routes", [])
interface["device"] = ifinfo.get("device", None)
if (
ifinfo.get("macaddr", None)
and ifinfo.get("macaddr", None) != "00:00:00:00:00:00"
):
interface["mac"] = ifinfo.get("macaddr", None)
interface["enabled"] = ifinfo.get("enabled", None)
interface["vlan_tag"] = ifinfo.get("vlan_tag", None)
interfaces[ifinfo.get("identifier", "")] = interface
# _LOGGER.debug(f"[get_interfaces] interfaces: {interfaces}")
return interfaces
Expand Down
20 changes: 18 additions & 2 deletions custom_components/opnsense/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,24 @@ def _handle_coordinator_update(self) -> None:
return
self._available = True
self._attr_extra_state_attributes = {}
for attr in ["ipaddr", "media"]:
self._attr_extra_state_attributes[attr] = interface[attr]
if prop_name == "status":
properties: list = [
"enabled",
"interface",
"device",
"ipv4",
"ipv6",
"mac",
"routes",
"gateways",
"media",
"vlan_tag",
]
else:
properties = ["interface", "device", "ipv4", "ipv6"]
for attr in properties:
if interface.get(attr, None):
self._attr_extra_state_attributes[attr] = interface[attr]
self.async_write_ha_state()

@property
Expand Down

0 comments on commit da891d5

Please sign in to comment.