djtimca / hagooglewifi

Home Assistant integration for Google Wifi systems.
Apache License 2.0
81 stars 27 forks source link

Platform googlewifi does not generate unique ID #45

Closed hoangtridung closed 2 years ago

hoangtridung commented 3 years ago

I' installed packet v 0.1.28 on HA 2021.9.4 and have error:

Logger: homeassistant.components.device_tracker
Source: helpers/entity_platform.py:566
Integration: Device tracker (documentation, issues)
First occurred: 2:55:25 PM (1 occurrences)
Last logged: 2:55:25 PM*

Platform googlewifi does not generate unique IDs. ID 7C49345E82411EAC9C4201489890ABDAC7FDE5315E27D47967FEFE9059BBC629 already exists - ignoring device_tracker.huawei_nova_3e_huawei_android_device

Search string 7C49345E82411EAC9C4201489890ABDAC7FDE5315E27D47967FEFE9059BBC629 only exist in 2 file in .storage folder: --> 1 hit in core.device_registry file as:

            {
                "config_entries": [
                    "7e581854fa76d168ca95dc18cd625e47"
                ],
                "connections": [],
                "identifiers": [
                    [
                        "googlewifi",
                        "7C49345E82411EAC9C4201489890ABDAC7FDE5315E27D47967FEFE9059BBC629"
                    ]
                ],
                "manufacturer": "Google",
                "model": "Connected Client",
                "name": "HUAWEI nova 3e (Huawei Android device)",
                "sw_version": null,
                "entry_type": null,
                "id": "bd7c1db0997ac82954dfd79c85ae16ba",
                "via_device_id": "75c2d23b42ef69a4eb4e2b60d7a9c40d",
                "area_id": null,
                "name_by_user": null,
                "disabled_by": null
            },

--> 2 hit in core.entity_registry file as:

            {
                "entity_id": "device_tracker.huawei_nova_3e_huawei_android_device",
                "config_entry_id": "7e581854fa76d168ca95dc18cd625e47",
                "device_id": "bd7c1db0997ac82954dfd79c85ae16ba",
                "area_id": null,
                "unique_id": "7C49345E82411EAC9C4201489890ABDAC7FDE5315E27D47967FEFE9059BBC629",
                "platform": "googlewifi",
                "name": null,
                "icon": null,
                "disabled_by": null,
                "capabilities": null,
                "supported_features": 0,
                "device_class": null,
                "unit_of_measurement": null,
                "original_name": "HUAWEI nova 3e (Huawei Android device)",
                "original_icon": "mdi:wifi"
            },
            ...

            {
                "entity_id": "switch.huawei_nova_3e_huawei_android_device",
                "config_entry_id": "7e581854fa76d168ca95dc18cd625e47",
                "device_id": "bd7c1db0997ac82954dfd79c85ae16ba",
                "area_id": null,
                "unique_id": "7C49345E82411EAC9C4201489890ABDAC7FDE5315E27D47967FEFE9059BBC629",
                "platform": "googlewifi",
                "name": null,
                "icon": null,
                "disabled_by": "integration",
                "capabilities": null,
                "supported_features": 0,
                "device_class": null,
                "unit_of_measurement": null,
                "original_name": "HUAWEI nova 3e (Huawei Android device)",
                "original_icon": "mdi:wifi"
            },

I guest 2 entity : switch and device_tracker are conflict by using the same unique_id": "7C49345E82411EAC9C4201489890ABDAC7FDE5315E27D47967FEFE9059BBC629". How to troubleshoot this problem ?

Thank for any advice !!!

KapJI commented 3 years ago

It seems entity type should be included in the unique id. Although changing that, will cause all devices will be unmatched after the update and will get new entity ids.

djtimca commented 3 years ago

I had this once previously, but I can't replicate it in my environment now. I believe I removed both conflicting entries from the entity_registry and then rebooted and it rediscovered correctly and added the devices back with unique IDs.

hoangtridung commented 3 years ago

I removed all Google-Wifi entries from 2 registry file, re-installed integration. But it's unsuccessful.

KapJI commented 3 years ago

Both switch and device_tracker which inherit GoogleWifiEntity use device_id for unique id, that's probably not right: https://github.com/djtimca/hagooglewifi/blob/83ec434ae266fe0e2831157bd6172f75dff19faf/custom_components/googlewifi/__init__.py#L269 https://github.com/djtimca/hagooglewifi/blob/83ec434ae266fe0e2831157bd6172f75dff19faf/custom_components/googlewifi/__init__.py#L275-L278

Switch: https://github.com/djtimca/hagooglewifi/blob/83ec434ae266fe0e2831157bd6172f75dff19faf/custom_components/googlewifi/switch.py#L67 https://github.com/djtimca/hagooglewifi/blob/83ec434ae266fe0e2831157bd6172f75dff19faf/custom_components/googlewifi/switch.py#L80

Device tracker: https://github.com/djtimca/hagooglewifi/blob/83ec434ae266fe0e2831157bd6172f75dff19faf/custom_components/googlewifi/device_tracker.py#L43

djtimca commented 3 years ago

I would have to go back and chase it out, but I'm pretty sure the unique id is by platform - HA actually differentiates between entity types when looking at the unique ID so you don't need to add that to the ID in the component.

If that wasn't the case then everyone with this integration would have the same issue and it is working correctly with the same unique ID in most installs including mine.

KapJI commented 3 years ago

Yeah, I think you're right: https://github.com/home-assistant/core/blob/065e858a032f1315f702ccc19b7c23a8c5015025/homeassistant/helpers/entity_registry.py#L265

After looking at HA code the problem seems that device_tracker.huawei_nova_3e_huawei_android_device already exists or that entity ID isn't available for use:

https://github.com/home-assistant/core/blob/065e858a032f1315f702ccc19b7c23a8c5015025/homeassistant/helpers/entity_platform.py#L545-L568

Not sure why though. Maybe there's some other integration which added entity with the same entity id?

hoangtridung commented 3 years ago

I don't have any device_tracker.huawei_nova_3e_huawei_android_device before. GG Wifi have ~ 70 connected devices and them are disabled while this integration add to HA. Enabling any entity have the same error.

djtimca commented 3 years ago

Does the same thing happen if you completely remove the integration and then re-install it?

hoangtridung commented 3 years ago

Yes I remove the integration completely. Are there the place to check entity_id on HASS outside core.registry files ?

djtimca commented 3 years ago

It is just on those files. If you remove the integration can you confirm the entities are removed from your core files?

hoangtridung commented 3 years ago

Yes. It's move to section "deleted_devices" in file core.device_registry I manual delete those entry as follow:

            }
        ],
        "deleted_devices": []
    }
}

In file core.entity_registry was clear.

djtimca commented 3 years ago

Is the behaviour any different if you add the integration with devices enabled?

hoangtridung commented 3 years ago

OOh, adding the integration as All Device Enabled --> Successful But all device_tracker entities always not_home. I's tried to disconnect and reconnect WIFI, but it's unsuccessful.

hoangtridung commented 3 years ago

More info: Disable any entity and Re-enable it --> The error: Platform googlewifi does not generate unique IDs ... return

djtimca commented 3 years ago

I am unable to replicate this behaviour in my environments. When I add the integration I get home and not_home correctly on device trackers and I can enable and disable entities with no issues.

If I add the integration with entities disabled I can enable them without issue.

What version of HA are you currently running? I tested with 2021.9.4 and 2021.10.0.dev0 and both worked correctly.

hoangtridung commented 3 years ago

The new error from HASS log:

This error originated from a custom integration.

Logger: homeassistant.config_entries
Source: custom_components/googlewifi/__init__.py:61
Integration: Google WiFi Integration (documentation, issues)
First occurred: 5:57:30 PM (1 occurrences)
Last logged: 5:57:30 PM

Error setting up entry Google Wifi for googlewifi
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 304, in async_setup
    result = await component.async_setup_entry(hass, self)  # type: ignore
  File "/config/custom_components/googlewifi/__init__.py", line 61, in async_setup_entry
    await api.connect()
  File "/usr/local/lib/python3.9/site-packages/googlewifi/__init__.py", line 171, in connect
    success = await self.get_api_token()
  File "/usr/local/lib/python3.9/site-packages/googlewifi/__init__.py", line 148, in get_api_token
    await self.get_access_token()
  File "/usr/local/lib/python3.9/site-packages/googlewifi/__init__.py", line 140, in get_access_token
    response = await self.post_api(url, headers, payload)
  File "/usr/local/lib/python3.9/site-packages/googlewifi/__init__.py", line 37, in post_api
    async with self._session.post(
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 619, in _request
    break
  File "/usr/local/lib/python3.9/site-packages/aiohttp/helpers.py", line 656, in __exit__
    raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError
szamfirov commented 2 years ago

I'm experiencing the same as @hoangtridung.

I've just raised https://github.com/djtimca/googlewifi-api/pull/4 which will solve the timeout issue.

RynoM commented 2 years ago

For future readers: After having the same issue, deleting the entry with the specific ID in core.device_registry and re-installing the add-on, the problem resolved itself for me.