tomaae / homeassistant-mikrotik_router

Mikrotik router integration for Home Assistant
Apache License 2.0
292 stars 48 forks source link

Exception in _async_process_device_data when dispatching 'device_tracker_connected_device_registered' #280

Closed Zarox666 closed 1 year ago

Zarox666 commented 1 year ago

Describe the issue

Device Tracker not working, as Exception is thrown when Router does not report an IP-Address (yet?).

How to reproduce the issue

In my Scenario this happens frequently. I have 3 Mikrotik Routers. 2 x RBD53iG-5HacD2HnD 1 x 2011UAS-2HnD From what I could determine so far the older 2011UAS-2HnD seems not to report an IP-Address towards HA frequently while the more modern ones do. However this seems to be working at times and in the Webfig UI the Device allways show with an IP-Address in the Wireless Registration.

Expected behavior

This is about making the Mikrotik Router Integration for HomeAssistant more resiliant, as it is possible (and legitimtate) to have a device in an Ethernet Network, that does not have an IP-Address. Thus I would expect the integration to handle an unknown IP-address gracefully. Maybe throwing a warning as it might be unexpected, but it should not catastrophicaly abort, and thus not feeding the device tracker.

Screenshots

image

Software versions

Diagnostics data

From the Debug Log:

2023-04-28 08:43:26.829 ERROR (MainThread) [homeassistant.helpers.event] Error while processing event <Event state_changed[L]: entity_id=device_tracker.waschmaschine_3, old_state=None, new_state=<state device_tracker.waschmaschine_3=home; source_type=router, ip=unknown, mac=XX:XX:XX:XX:XX:XX, host_name=XX:XX:XX:XX:XX:XX, attribution=Data provided by Mikrotik, interface=wlan_iot, source=wireless, last_seen=Now, icon=mdi:lan-connect, friendly_name=Waschmaschine @ 2023-04-28T08:43:26.764280+02:00>> for domain device_tracker
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 431, in _async_dispatch_domain_event
    hass.async_run_hass_job(job, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 593, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/dhcp/__init__.py", line 318, in _async_process_device_event
    self._async_process_device_state(event.data["new_state"])
  File "/usr/src/homeassistant/homeassistant/components/dhcp/__init__.py", line 338, in _async_process_device_state
    self.async_process_client(ip_address, hostname, _format_mac(mac_address))
  File "/usr/src/homeassistant/homeassistant/components/dhcp/__init__.py", line 157, in async_process_client
    made_ip_address = make_ip_address(ip_address)
  File "/usr/local/lib/python3.10/ipaddress.py", line 54, in ip_address
    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')
ValueError: 'unknown' does not appear to be an IPv4 or IPv6 address
2023-04-28 08:43:26.834 ERROR (MainThread) [homeassistant.helpers.event] Error while processing event <Event state_changed[L]: entity_id=device_tracker.miner_ant1, old_state=None, new_state=<state device_tracker.miner_ant1=home; source_type=router, ip=unknown, mac=XX:XX:XX:XX:XX:XX, host_name=XX:XX:XX:XX:XX:XX, attribution=Data provided by Mikrotik, interface=wlan_iot, source=wireless, last_seen=Now, icon=mdi:lan-connect, friendly_name=Miner-Ant1 @ 2023-04-28T08:43:26.772618+02:00>> for domain device_tracker
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 431, in _async_dispatch_domain_event
    hass.async_run_hass_job(job, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 593, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/dhcp/__init__.py", line 318, in _async_process_device_event
    self._async_process_device_state(event.data["new_state"])
  File "/usr/src/homeassistant/homeassistant/components/dhcp/__init__.py", line 338, in _async_process_device_state
    self.async_process_client(ip_address, hostname, _format_mac(mac_address))
  File "/usr/src/homeassistant/homeassistant/components/dhcp/__init__.py", line 157, in async_process_client
    made_ip_address = make_ip_address(ip_address)
  File "/usr/local/lib/python3.10/ipaddress.py", line 54, in ip_address
    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')
ValueError: 'unknown' does not appear to be an IPv4 or IPv6 address

Traceback/Error logs

See above

Additional context

This has worked once. I just realized two days ago, that my mobile shows as away when connected to the 2011UAS-2HnD while it shows as home if connected to any of the two RBD53iG-5HacD2HnD. One of the RBD53iG-5HacD2HnD is also running RouterOS v.6.49.7 while the other is running v7.6. When I started with the Mikrotik RouterOS Integration the 2011UAS-2HnD was my only Mikrotik WLAN AP and the device tracker worked. I cannot say when it stopped working, but neither reboot of the router nor reloading of the integration makes the device tracker work. Also there has been no change in RouterOS since this started. But Imho the Integration should be able to handle ip=unknown.

tomaae commented 1 year ago

errors you are showing are from dhcp discovery integration. Crosscheck if you really have issue with mikrotik integration

Zarox666 commented 1 year ago

dhcp discovery integration? What is that?... a HA Integration? I just checked through my setup but I could not find any Integration or HACS addon with that name!? Where would I need to go to open a change request for that? HA itself?

The 2011UAS-2HnD is acting as DHCP Server for two networks (while the other two routers do not), but the main Network gets its DHCP Addresses from a Windows Domain Contoller. My Mobile is getting the IP from the DC and is working just fine, is reported in the Wifi Registrations of the Mikrotik but still showing as away in HA.

Zarox666 commented 1 year ago

Ah! I think I found what you mean: https://www.home-assistant.io/integrations/dhcp/

I was not aware that HA loads so much stuff by default. How can I disable it?

tomaae commented 1 year ago

I'm not really able to help you with dhcp discovery integration, I never looked into it. But it getting "unknown" may mean that your DNS server is replying with such. So I would start with checking those. It may be a real problem in your network rather then a bug, better be safe then sorry later ;)

Zarox666 commented 1 year ago

Thanks for your help... I figured out a few things in the meantime :-)

That my DNS Server is responding with unknown for these Devices is expected. That my VLAN Isolation is not working is not expected :-) Need to look into that.

Also that disabling the dhcp integration (plus a few others out of default_config) solved my MemoryLeak was also not expected. And at the moment the Device Tracker correctly reports my mobile as home even when connected to the 2011UAS-2HnD. So lets close it for now, and sorry for all the fuzz. Nevertheless you helped me a lot! Thanks!

tomaae commented 1 year ago

I did nothing, but glad to hear it :)