iMicknl / ha-nest-protect

Nest Protect integration for Home Assistant. This will allow you to integrate your smoke, heat, co and occupancy status real-time in HA.
MIT License
338 stars 64 forks source link

Errors in ` _async_subscribe_for_data` #347

Open sambeetm opened 3 months ago

sambeetm commented 3 months ago

The problem

Getting these errors in the logfile..

AttributeError: 'list' object has no attribute 'object_key' 2024-07-24 12:16:33.179 ERROR (MainThread) [custom_components.nest_protect] Unknown exception. Please create an issue on GitHub with your logfile. Updates paused for 5 minutes. Traceback (most recent call last): File "/config/custom_components/nest_protect/init.py", line 208, in _async_subscribe_for_data dict(b, **buckets.get(b.object_key, {})) for b in [data.updated_buckets]

What version of this integration (ha-nest-protect) has the issue?

v0.4.0b5

What version of Home Assistant Core has the issue?

2024.7.3

Device / Model

Topaz-2.7

Diagnostics information

No response

Home Assistant log

Logs ``` AttributeError: 'list' object has no attribute 'object_key' 2024-07-24 12:16:33.179 ERROR (MainThread) [custom_components.nest_protect] Unknown exception. Please create an issue on GitHub with your logfile. Updates paused for 5 minutes. Traceback (most recent call last): File "/config/custom_components/nest_protect/__init__.py", line 208, in _async_subscribe_for_data dict(b, **buckets.get(b.object_key, {})) for b in [data.updated_buckets] ```

Additional information

No response

jamesbont007 commented 3 months ago

+1

MikeBP13 commented 3 months ago

+1

helmerzNL commented 3 months ago

+1

dirixmjm commented 2 months ago

+1

FeralSquid commented 2 months ago

+1

dirixmjm commented 2 months ago

I'm not very familiar with python, but I believe the bug is the brackets around 'data.updated_buckets' here:

    objects = [
        dict(b, **buckets.get(b.object_key, {})) for b in [data.updated_buckets]
    ]

Traceback (most recent call last): File "/home/homeassistant/.homeassistant/custom_components/nest_protect/init.py", line 208, in _async_subscribe_for_data dict(b, **buckets.get(b.object_key, {})) for b in data.updated_buckets ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'Bucket' object is not iterable

iMicknl commented 2 months ago

Thanks all! Known issue, no need to keep reporting +1 (everyone on the latest version faces this 😉).

At the moment I don't have time to work on this unfortunately, but hope to make some time in the coming weeks. In the mean-time, contributions are appreciated.

FeralSquid commented 2 months ago

yea, I'm having a hard time googling around to help parse that line of code and how it might be fixed, but based on the previous version and the comment above, I suspect the non-one-liner way to write what the intent is there would be:

objects: list[Bucket] = []
for b in updated_buckets:
    new_data = buckets.get(b["object_key"], {})
    if (new_data != {}):
        objects.append(cast(Bucket, new_data)) # not sure if cast is necessary here?
    else:
        objects.append(b) # I believe the intent is to keep around the old data if it isn't in the new result?

does that look right?

ScottESanDiego commented 1 month ago

does that look right?

That looks right to my eyes at first blush (changing updated_buckets to data.updated_buckets of course). Have you had any luck with it working @FeralSquid ? (I don't see a PR, so assuming no time yet)

therealmrfox commented 1 month ago

Code above isn't quite right either:

Traceback (most recent call last):
  File "/config/custom_components/nest_protect/__init__.py", line 223, in _async_subscribe_for_data
    new_data = buckets.get(b["object_key"], {})
                           ~^^^^^^^^^^^^^^
TypeError: 'Bucket' object is not subscriptable
ScottESanDiego commented 1 month ago

Here's what seems to be working for me... I'm not sure why it works, but no errors and so far somewhat timely occupancy detection (which is what I'm trying to get):

        # Update buckets with new data, to only receive new updates
        buckets = {d["object_key"]: d for d in result["objects"]}

        LOGGER.debug(buckets)

        objects: list[Bucket] = []
        for b in data.updated_buckets:
            objects.append(b) # I believe the intent is to keep around the old data if it isn't in the new result?

        data.updated_buckets = objects

I'm not 100% convinced this is the right solution (or substantially any different than the original code!).

ScottESanDiego commented 1 month ago

Submitted a PR for this https://github.com/iMicknl/ha-nest-protect/pull/355 (on the beta branch, although I think the same would apply to main)

therealmrfox commented 1 month ago

EDIT: Err, never mind. I somehow ended up with 2 integration entries - 1 working happily, and 1 with outdated credentials spamming the logs. Errors went away after deleting the old entry and rebooting.


With the patch above, I now get a different error:

Traceback (most recent call last):
  File "/config/custom_components/nest_protect/__init__.py", line 164, in _async_subscribe_for_data
    auth = await entry_data.client.get_access_token()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/nest_protect/pynest/client.py", line 91, in get_access_token
    await self.get_access_token_from_cookies(self.issue_token, self.cookies)
  File "/config/custom_components/nest_protect/pynest/client.py", line 158, in get_access_token_from_cookies
    raise BadCredentialsException(
custom_components.nest_protect.pynest.exceptions.BadCredentialsException: USER_LOGGED_OUT - No active session found.

Refreshing my issue_token and cookies didn't fix that.

The funny thing is that the integration still works ok - Presence detection happens within a few seconds (with a 10-minute timeout before going back to clear, but I think that's expected?)

Maybe that _async_subscribe_for_data business isn't needed at all?

thargy commented 1 month ago

@iMicknl in #358 you closed as duplicate saying,

It might be fixed in the main branch of this integration, but I haven't been able to test this myself yet.

However, the main branch has had no commits since January, and the beta hasn't had any commits since August. Where did you mean for us to look?

Perhaps #355?

I have applied the change in 4a12184516ad8dc5034ec8be0e65ced3098213ec manually for now, and can confirm that, so far, the log spam has disappeared.

iMicknl commented 1 month ago

@thargy sorry, you are correct. It is indeed #355, I thought we already had this in the latest beta branch.

I will merge it now and do a new release.

ScottESanDiego commented 3 weeks ago

@iMicknl Ideas on how to fix this the right way? I didn't notice the communication side-effect with my kludge.