travisghansen / hass-opnsense

OPNsense integration with Home Assistant
213 stars 29 forks source link

[Bug]: "DHCP Leases All" sensor has no data #237

Closed o0shojo0o closed 1 week ago

o0shojo0o commented 1 week ago

What happened?

The new "DHCP Leases All" sensor has no data image

hass-opnsense Version

v0.3.5-beta.1

OPNsense Firmware

24.7.5_3

Home Assistant Version

2024.10.0

Relevant logs

Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: custom_components.opnsense.pyopnsense
Quelle: custom_components/opnsense/pyopnsense/__init__.py:117
Integration: OPNsense (Dokumentation, Probleme)
Erstmals aufgetreten: 20:41:45 (31 Vorkommnisse)
Zuletzt protokolliert: 20:49:53

Error in get_dhcp_leases. ValueError: '' does not appear to be an IPv4 or IPv6 address

Additional Details

I use the ISC DHCPv4

alexdelprete commented 1 week ago

I'm on HA 2024.10.1 - OPNsense 24.7.5_3, sensor working fine:

image

alexdelprete commented 1 week ago

I use the ISC DHCPv4

Dennis, could you please enable debug log on the integration and reload the integration, then attach it here so @Snuffy2 can take a look. Thanks.

o0shojo0o commented 1 week ago

I think this is due to my MAC assignments without an IP address image

alexdelprete commented 1 week ago

I think this is due to my MAC assignments without an IP address

I don't understand: you use DHCP without assigning IPs?

o0shojo0o commented 1 week ago

This allows you to link the MAC to a name.

alexdelprete commented 1 week ago

You do that for Firewall rules? Couldn't you use Aliases for that?

o0shojo0o commented 1 week ago

I do it so that the name and not just the MAC appears in the analysis tools and Adguard

alexdelprete commented 1 week ago

Reverse DNS lookups don't work? I use AGH and hostnames are resolved fine through reverse DNS lookups.

alexdelprete commented 1 week ago

@Snuffy2 if the IP address field is empty, it shouldn't be counted as a lease.

Snuffy2 commented 1 week ago

Are there entries without an IP currently showing/being counted?

alexdelprete commented 1 week ago

Are there entries without an IP currently showing/being counted?

Sorry: counted = processed. An error is being thrown in HA (get_dhcp_leases), check OP.

I see this in the code:

        for if_subnet in sorted_leases.values():
            sorted_if: list = sorted(
                if_subnet, key=lambda x: normalize_ip(x["address"])
            )
            if_subnet: list = sorted_if

What happens if address is None?

Snuffy2 commented 1 week ago

Gotcha. Sorry, looking on a phone and missed the error in the OP. Yeah I'll handle if no IP. Does it still count as a lease then?

Also, Kea does not currently show whether a lease is static or dynamic. ISC does/did. I wanted to show this in the attributes but for Kea will need to also pull in the reservation list separately and merge.

alexdelprete commented 1 week ago

Does it still count as a lease then?

No it doesn't. And if you ask me, a proper DHCP implementation shouldn't allow an empty IP address. I would have to look to the RFC to be sure, but a DHCP service main purpose is to assign IP addresses, having an empty IP makes no sense to me.

Also, Kea does not currently show whether a lease is static or dynamic. ISC does/did.

If I interpret the docs correctly, the Leases DHCPv4 should only contain dynamic leases.

image

Snuffy2 commented 1 week ago

Well the Kea Leases page and the Kea API definitely return active static leases as well as the active dynamic ones.

alexdelprete commented 1 week ago

Well the Kea Leases page and the Kea API definitely return active static leases as well as the active dynamic ones.

I just migrated from ISC DHCP to ISC Kea (migrated all static leases through this tool), and you're right, the official docs are wrong: that page shows every kind of lease, without a column distinguishing static/dynamic.

What about the REST API? Is there a type field in the json?

Snuffy2 commented 1 week ago

Nope. As I said, I'd have to also pull in the reservation list separately and merge them together. Examples of each dhcp entry type are currently as comments in pyopnsense/init.py around line 700.

alexdelprete commented 1 week ago

I'm wondering if the info is there but just not in the REST API, might be worth asking OPNsense devs in that case.