tomaae / homeassistant-mikrotik_router

Mikrotik router integration for Home Assistant
Apache License 2.0
301 stars 50 forks source link

[Bug] Devicetracker for turned off device still shows home #265

Closed mvdwetering closed 1 year ago

mvdwetering commented 1 year ago

Describe the issue

I noticed that a device tracker for a VM I turned off was still showing up as "home" and last_seen keeps saying Now.

This is most likely because I still have a disabled static lease for that entry (Home Assistant Hyper V) and a new active static lease for the new install on a different machine (Home Assistant Proxmox), but using the same IP as the disabled one (I kept the old static lease in case I want/need to switch back to the older install on the other machine).

How to reproduce the issue

Expected behavior

Devices that are not on the network show as away/unavailable

Screenshots

image

Software versions

Diagnostics data

config_entry-mikrotik_router-9959853863f01820b498788cbc1eba7f (hashed macs).json.txt

Traceback/Error logs

No errors

Additional context

tomaae commented 1 year ago

do they have different mac addresses?

mvdwetering commented 1 year ago

Ow whoops, went a bit agressive on mac address hiding. Yes they are diffent macs. Both in the DHCP leases and the entity attributes shown in Home Assistant. I did not clone a VM or something.

tomaae commented 1 year ago

did you restarted HA since, or is it still running since you recycled the IP?

mvdwetering commented 1 year ago

HA Core was restarted, not sure about the OS

tomaae commented 1 year ago

try installing master version using HACS and test if it works

mvdwetering commented 1 year ago

It failed with the following error.

2023-01-18 21:24:05.085 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry RB4011iGS for mikrotik_router
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 382, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/config/custom_components/mikrotik_router/__init__.py", line 49, in async_setup_entry
    await controller.async_update()
  File "/config/custom_components/mikrotik_router/mikrotik_controller.py", line 655, in async_update
    await self.async_process_host()
  File "/config/custom_components/mikrotik_router/mikrotik_controller.py", line 2105, in async_process_host
    if not self.data["host"][uid]["source"]["enabled"]:
KeyError: 'C8:60:00:DE:B3:53'

I added the last command from the debug logging (I redacted hostnames and comments, on a sidenote there is a number hostname in there without 'quotes' around it which seems unexpected) .

It seems to fail on the first entry already. Untitled-1.log

tomaae commented 1 year ago

ah yea, it was my mistake. try again please.

mvdwetering commented 1 year ago

Does not seem to make a difference, devicetracker for the diabled VM/static lease still says Home

source_type: router
ip: 192.168.178.29
mac: __REDACTED__
attribution: Data provided by Mikrotik
interface: bridge
source: dhcp
last_seen: Now
icon: mdi:lan-connect
friendly_name: Home Assistant
tomaae commented 1 year ago

Check that it was updated to current master and integration was reloaded. Disabled DHCP entries are skipped now, so it should not appear.

You can check that in diagnostics. It should show new enabled property for all DHCP entries.

mvdwetering commented 1 year ago

I had already doublechecked, but to make sure I checked code this time and it has the latest changes from master. The entry still appears, but it does show enabled:false in diagnostics.

        "00:15:5D:8B:81:11": {
          "mac-address": "**REDACTED**",
          "active-mac-address": "**REDACTED**",
          "address": "**REDACTED**",
          "active-address": "**REDACTED**",
          "host-name": "homeassistant",
          "status": "waiting",
          "last-seen": "3w2d3h44m8s",
          "server": "defconf",
          "comment": "Home Assistant (Hyper-V)",
          "enabled": false,
          "interface": "bridge"
        },

Did some more experimentation and noticed that after reloading the integration the entity for the entry that is disabled now always initially gets reported as Away, but after 30 seconds it changes to Home. The other entity becomes Home immediately after reloading. This seems to be consistent after trying it 5 times with the master version.

On the 2.1.2 version it always shows Home immediately (also tried several times), so something did change.

Not sure if that give a hint of something or not.

tomaae commented 1 year ago

Yes, there was still update from DHCP left. Can you try now?

mvdwetering commented 1 year ago

Now it reports Away always 👍

tomaae commented 1 year ago

excellent :)