tomaae / homeassistant-mikrotik_router

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

[Bug] Duplicate Device Trackers being created after v2.1 upgrade #236

Closed davidmarah closed 2 years ago

davidmarah commented 2 years ago

Describe the issue

Having just updated from v2.0 to v2.1, I'm starting to see duplicate device_tracker entities (and associated HA devices) being created after each Home Assistant restart. The original entities continue to be updated with the correct status, but there's a series of duplicates created that always have the status 'Away' and cannot be removed.

I've removed and re-added the integration and the issue still persists - on adding there is one copy of each device, subsequent restarts create a duplicate each time.

How to reproduce the issue

Restart Home Assistant using Developer Tools > YAML > Restart

Expected behaviour

Device tracking works as expected, but the large number of duplicates should not be there.

Screenshots

As shown in the device screen: image

Software versions

Traceback/Error logs

I have a large number of tracked devices (over 100), so turning on verbose logging resulted in huge logs and I didn't see anythign at first glance.

I did spot the following in the .storage/core.device_registry file.

The initial device works as expected (this is an ESPhome device, so multiple config entries, and many details supplied from there):

    "config_entries": [
      "dbad203be7461cf16573210d2fe07ac9",
      "dccae4d8952f90f299eb1a29ffcd9907"
    ],
    "connections": [
      [
        "mac",
        "4c:eb:d6:0f:f1:df"
      ]
    ],
    "identifiers": [],
    "manufacturer": "espressif",
    "model": "esp01_1m",
    "name": "study_greenlight",
    "sw_version": "2022.6.3 (Aug 11 2022, 12:58:21)",
    "hw_version": null,
    "entry_type": null,
    "id": "fd5ea92dab2c66f04f9eb33d1e7cffa6",
    "via_device_id": "19fa338b73ee0cba17c35a0e2d2dc018",
    "area_id": "spareroom",
    "name_by_user": null,
    "disabled_by": null,
    "configuration_url": null

The first duplicate take this format:

    "config_entries": [
      "dccae4d8952f90f299eb1a29ffcd9907"
    ],
    "connections": [
      [
        "mac",
        "ROUTER-HOST-4C:EB:D6:0F:F1:DF"
      ]
    ],
    "identifiers": [],
    "manufacturer": "",
    "model": null,
    "name": "study_greenlight.esphome.1.claycroft.net",
    "sw_version": null,
    "hw_version": null,
    "entry_type": null,
    "id": "40fe2a8e6b9704235705aba47bc7a53d",
    "via_device_id": "19fa338b73ee0cba17c35a0e2d2dc018",
    "area_id": null,
    "name_by_user": null,
    "disabled_by": null,
    "configuration_url": null

and the third:

    "config_entries": [
      "dccae4d8952f90f299eb1a29ffcd9907"
    ],
    "connections": [
      [
        "mac",
        "ROUTER-HOST-ROUTER-HOST-4C:EB:D6:0F:F1:DF"
      ]
    ],
    "identifiers": [],
    "manufacturer": "",
    "model": null,
    "name": "study_greenlight.esphome.1.claycroft.net",
    "sw_version": null,
    "hw_version": null,
    "entry_type": null,
    "id": "c45f362489f4d100dd08228fc35147ad",
    "via_device_id": "19fa338b73ee0cba17c35a0e2d2dc018",
    "area_id": null,
    "name_by_user": null,
    "disabled_by": null,
    "configuration_url": null

The IDs in connections continue to grow in the same format, with each new one adding ROUTER-HOST to the front. I've got up to 7 duplicates in the same format before removing and reinstalling the integration.

Similarly in .storage/entity_registry we have:

    "area_id": null,
    "capabilities": null,
    "config_entry_id": "dccae4d8952f90f299eb1a29ffcd9907",
    "device_class": null,
    "device_id": "fd5ea92dab2c66f04f9eb33d1e7cffa6",
    "disabled_by": null,
    "entity_category": "diagnostic",
    "entity_id": "device_tracker.study_greenlight_esphome_1_claycroft_net",
    "hidden_by": null,
    "icon": null,
    "id": "cdd28482d7de736bb30e24bdd18adb57",
    "has_entity_name": false,
    "name": null,
    "options": {},
    "original_device_class": null,
    "original_icon": "mdi:lan-connect",
    "original_name": "study_greenlight.esphome.1.claycroft.net",
    "platform": "mikrotik_router",
    "supported_features": 0,
    "unique_id": "router-host-4c:eb:d6:0f:f1:df",
    "unit_of_measurement": null

followed by:

    "area_id": null,
    "capabilities": null,
    "config_entry_id": "dccae4d8952f90f299eb1a29ffcd9907",
    "device_class": null,
    "device_id": "40fe2a8e6b9704235705aba47bc7a53d",
    "disabled_by": null,
    "entity_category": "diagnostic",
    "entity_id": "device_tracker.study_greenlight_esphome_1_claycroft_net_2",
    "hidden_by": null,
    "icon": null,
    "id": "8f5eb58db85f08674358c70939e7f551",
    "has_entity_name": false,
    "name": null,
    "options": {},
    "original_device_class": null,
    "original_icon": "mdi:lan-disconnect",
    "original_name": "study_greenlight.esphome.1.claycroft.net",
    "platform": "mikrotik_router",
    "supported_features": 0,
    "unique_id": "router-host-router-host-4c:eb:d6:0f:f1:df",
    "unit_of_measurement": null

and:

    "area_id": null,
    "capabilities": null,
    "config_entry_id": "dccae4d8952f90f299eb1a29ffcd9907",
    "device_class": null,
    "device_id": "c45f362489f4d100dd08228fc35147ad",
    "disabled_by": null,
    "entity_category": "diagnostic",
    "entity_id": "device_tracker.study_greenlight_esphome_1_claycroft_net_3",
    "hidden_by": null,
    "icon": null,
    "id": "281e66898e088827bd5daae20a11652b",
    "has_entity_name": false,
    "name": null,
    "options": {},
    "original_device_class": null,
    "original_icon": "mdi:lan-disconnect",
    "original_name": "study_greenlight.esphome.1.claycroft.net",
    "platform": "mikrotik_router",
    "supported_features": 0,
    "unique_id": "router-host-router-host-router-host-4c:eb:d6:0f:f1:df",
    "unit_of_measurement": null

Again, it looks like the unique_id field grows by pre-pending router-host each time.

Additional context

I have another four Mikrotik devices on the network and installed in Home Assistant, although only this one does device tracking (the others are switches and APs using CAPsMAN).

tomaae commented 2 years ago

thanks for detailed analysis, that really helped fixed in 2.1.1

davidmarah commented 2 years ago

no problem. tested and working in 2.1.1

thanks again