DHCP Discovery testing and implementation

Hi :wave:!

Can someone help me figuring out how DHCP discovery works?

On my dev environment it doesn’t seem to work. Its a macOS venv installation and I get [homeassistant.components.dhcp] Cannot watch for dhcp packets without root or CAP_NET_RAW: Can't attach the BPF filter ! when I start HA.
I’ve tried it on a Windows machine running WSL too - with the same error.
I’ve even tried to run a Dev-Container, but I don’t quite know what I’m doing there and it seems it doesn’t get any DHCP requests forwarded.

How can I test this?


About the implementation: I've added the OUI to the manifest and ran `script.hassfest` so it is added to generated/dhcp and added `async def async_step_dhcp()` to my config_flow.py.
    async def async_step_dhcp(self, discovery_info: DhcpServiceInfo) -> FlowResult:
        """Handle a flow initiated by the DHCP client."""
        discovery_input = {CONF_HOST: discovery_info.ip}
        self._async_abort_entries_match(discovery_input)
        return await self.async_step_user(user_input=discovery_input)

Can I just use DhcpServiceInfo data and forward it to self.async_step_user() - this would create the config_entry - or do I need to add some kind of confirmation step (I’ve seen this on couple of integrations code)?

Regards, Farmio

It basically sniff the network for DHCP packets, which requires elevated (root) privileges.

Thank you for your reply.
I know what it does, or is supposed to do, but I can’t get it running on my systems.
On WSL I tried sudo setcap cap_net_raw+ep /usr/bin/python3 Does remove the log message in startup, but still nothing gets discovered.
I’ve yet to try this on macOS.

Can I somehow run Hass as root? Sounds kind of wrong to me…

I doubt it will work on WSL.
CAP_NET_RAW is a privilege given to a docker container.

There is no Docker involved in my setups.
I tried to run sudo hass on macOS now, but it still gives me
[homeassistant.components.dhcp] Cannot watch for dhcp packets: Can't attach the BPF filter ! :confused:

So on macOS using an ubuntu aarch64 VM and installing HA there (using sudo setcap 'cap_net_raw+ep' /usr/bin/python3.8) works and I can test DHCP discovery now :tada: