alandtse / alexa_media_player

This is a custom component to allow control of Amazon Alexa devices in Home Assistant using the unofficial Alexa API.
Apache License 2.0
1.36k stars 263 forks source link

Alexa Media Player fails to initialize after restarting HA 2024.6.3 due to partitioned cookie #2288

Open danielbrunt57 opened 2 weeks ago

danielbrunt57 commented 2 weeks ago

IMPORTANT: Please search the issues, including closed issues, and the FAQ before opening a new issue. The template is mandatory; failure to use it will result in issue closure.

Describe the bug

Alexa Media Player is failing to initialize after restarting HA 2024.6.3. If I reload the integration, all is well.

To Reproduce

Restart Home assistant 2014.6.3 and observe that the integration failed to load.

Expected behavior

It should load like it did on 2024.6.2 and older.

Screenshots

image

System details

Logs

home-assistant_2024-06-17T13-08-27.618Z.log

Additional context

Petro31 commented 1 week ago

I think the problem lies with HA's startup changes and blocking calls during integration import.

this is showing up in beta async_forward_entry_setup as something to avoid using and Alexa media player is using that call.

Well, I fixed that on my system and it didn't solve the problem.

danielbrunt57 commented 1 week ago

So this was your change?

image

danielbrunt57 commented 1 week ago

@Petro31 Are you also getting the partitioned cookie error?

Petro31 commented 1 week ago

@Petro31 Are you also getting the partitioned cookie error?

Yes but I haven't fixed that yet. That requires some more debugging.

The way to fix the beta errors and warnings is to swap to hass.config_entries.async_forward_entry_setups( and wrap component in a list [component]

https://github.com/alandtse/alexa_media_player/pull/2294


                    config_entry.async_create_task(
                        hass,
                        hass.config_entries.async_forward_entry_setups(
                            config_entry, [component]
                        )
                    )

or you could move it out of create tasks and await that.

                        await hass.config_entries.async_forward_entry_setups(
                            config_entry, [component]
                        )
danielbrunt57 commented 1 week ago

Yes but I haven't fixed that yet. That requires some more debugging.

I fixed my error with the cookies.py file from the new python 3.13 work where giles-v has a PR: Add support for 'partitioned' attribute in http.cookies #112714 which adds support for Cookies Having Independent Partitioned State (CHIPS) but now for some strange reason I no longer need it and do not have the error. I'm thinking it's somehow browser related but do not know how I rid myself of my partitioned cookie...

juan11perez commented 6 days ago

@danielbrunt57 thank you for your suggestion. I confirm that following the above (https://github.com/alandtse/alexa_media_player/issues/2288#issuecomment-2181877003 )recommendation fixed my problem

dragorex71 commented 5 days ago

For me it does not fix the issue. But i could not follow the exact instruction because of the cookies.py location. @danielbrunt57 said it is in

/usr/local/lib/python3.12/http/cookies.py

but i have it in

/usr/lib/python3.12/http/cookies.py

Menz01 commented 5 days ago

So for me, the only thing that worded was restoring back to version 2024.6.2 and that is where I stayed. Now with 2024.7.0 due to come out soon, I wonder if it will be addressed in it

wxman60 commented 5 days ago

Comment #2288 didn't work for me. The first few restarts everything was good. After the fourth restart it started failing again and has failed after subsequent restarts. I reverted back to 2024.6.2.

Strega-Technologies commented 5 days ago

same here on 2024.6.4: restoring back 2024.6.2 solves the issue. Hope HA team will fix this...?

danielbrunt57 commented 4 days ago

Comment #2288 didn't work for me. The first few restarts everything was good. After the fourth restart it started failing again and has failed after subsequent restarts. I reverted back to 2024.6.2.

Were you getting the 'partitioned' cookie error after the 4th restart? If so, then I suspect "the fix" somehow disappeared...

As I stated earlier, after fixing my partitioned cookie error via comment #2288, I now no longer need the fix and am running 2024.6.4 and the integration just fine . It's beyond me as to what's really going on and why...

wxman60 commented 4 days ago

@danielbrunt57 Sorry I didn't check for partitioned cookie error before I rolled back. I just tried again and restarted 6 times but have not gotten the error yet. Alexa Media player is still loading as it should. If I do get it later, I will check for error.

danielbrunt57 commented 4 days ago

There is something very strange going on here and even though I am trouble free now, I am still puzzled as to why this "partitioned cookie" thing has reared it's ugly head. Normally I like "cookies", but not this one!

danielbrunt57 commented 4 days ago

@danielbrunt57 Sorry I didn't check for partitioned cookie error before I rolled back. I just tried again and restarted 6 times but have not gotten the error yet. Alexa Media player is still loading as it should. If I do get it later, I will check for error.

Is that with or without the "enhanced" cookies,py file?

juan11perez commented 4 days ago

@danielbrunt57

My alexa instance had just stopped all together. Your suggested fix with edited py fixed it and continues to work after several restarts

danielbrunt57 commented 4 days ago

@danielbrunt57

My alexa instance had just stopped all together. Your suggested fix with edited py fixed it and continues to work after several restarts

I'm glad it helped but still don't understand why it was/is needed when I no longer need it....

Strega-Technologies commented 4 days ago

Various back and forth here: can someone be kind enough to give the exact steps to fix this issue ? Thanks in advance.

danielbrunt57 commented 4 days ago

Well, it's not a "fix" per se as no one so far really knows what is going on (myself included). If you are getting a "partitioned cookie" error then my # 2288 (comment) should get you running again.
But the question remains, why is partitioned cookie support all of a sudden even necessary. HA has changed something (a lot?) that I haven't been able to figure out,,,

Strega-Technologies commented 4 days ago

Thank you Daniel ! I could manage to reauthenticate and it works again. When you know more, keep us posted so that we know what is the right procedure to use on upcoming HA release.

wxman60 commented 4 days ago

@danielbrunt57 I am now getting the issue with Alexa Media Player loading on restart. I am also seeing the cookie partition issue.

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/config_entries.py", line 594, in async_setup result = await component.async_setup_entry(hass, self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/alexa_media/init.py", line 335, in async_setup_entry await login.login(cookies=await login.load_cookie()) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/alexapy/alexalogin.py", line 328, in load_cookie cookies = pickle.loads(await myfile.read()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/http/cookies.py", line 312, in setitem raise CookieError("Invalid attribute %r" % (K,)) http.cookies.CookieError: Invalid attribute 'partitioned'

danielbrunt57 commented 4 days ago

@danielbrunt57 I am now getting the issue with Alexa Media Player loading on restart. I am also seeing the cookie partition issue.

What all have you tried or installed since retrying? Are you on 2024.6.4? Is the "enhanced" cookies.py file still in place? Line 285 should be this:

        "partitioned": "Partitioned",
wxman60 commented 4 days ago

Yes, line 285 has this "partitioned": "Partitioned"

danielbrunt57 commented 4 days ago

Yes, line 285 has this "partitioned": "Partitioned"

and the file is "/usr/lib/python3.12/http/cookies.py"?

danielbrunt57 commented 4 days ago

or, if you are inside the homeassistant container, it's "/usr/local/lib/python3.12/http/cookies.py"

wxman60 commented 4 days ago

In the home assistant container at /usr/local/lib/python3.12/http/cookies.py, "partition": "Partitioned" is in the cookies.py file.

danielbrunt57 commented 4 days ago

File "/usr/local/lib/python3.12/http/cookies.py", line 312, in setitem raise CookieError("Invalid attribute %r" % (K,)) http.cookies.CookieError: Invalid attribute 'partitioned'

Line 312 in the original cookies.py file is: raise CookieError("Invalid attribute %r" % (K,))

Line 312 in the PR's cookies.py file is: if not K in self._reserved: and the raise CookieError("Invalid attribute %r" % (K,)) is line 313!

It would seem your system is somehow executing the original cookies.py which has the raise CookieError at line 312.

I'd suggest trying a full system reboot on your HA system.

image

danielbrunt57 commented 4 days ago

When I'm confused about where things are, I use find to search and print the full path:

cd /
find . -name cookies.py -print
wxman60 commented 4 days ago

Did a full reboot as you suggested and then did a find and these are the results. ~ cd /
➜ / find . -name cookies.py -print ./usr/lib/python3.12/site-packages/pip/_vendor/requests/cookies.py ./usr/lib/python3.12/http/cookies.py ➜ /

Looking at the cookies.py it has reverted back to an earlier version and the partition": "Partitioned" is not there.

danielbrunt57 commented 3 days ago

My container looks like this:

homeassistant:/# find . -name cookies.py -print
./root/.cache/uv/archive-v0/ibQyxfM70TC3Fi39bIe8P/requests/cookies.py
./root/.cache/uv/archive-v0/DbZOyLf3AuipJyun0BFT-/jaraco/net/http/cookies.py
./root/.cache/uv/archive-v0/RTXeQ7dvg7towOsFQRLcM/yt_dlp/cookies.py
./root/.cache/uv/archive-v0/KpoWgTwHHzDd6M7RnEhy2/future/backports/http/cookies.py
./root/.cache/uv/archive-v0/KpoWgTwHHzDd6M7RnEhy2/future/moves/http/cookies.py

./usr/local/lib/python3.12/http/cookies.py  <-- This is the only one I replaced as all the others do something different.

./usr/local/lib/python3.12/site-packages/requests/cookies.py
./usr/local/lib/python3.12/site-packages/jaraco/net/http/cookies.py
./usr/local/lib/python3.12/site-packages/yt_dlp/cookies.py
./usr/local/lib/python3.12/site-packages/future/backports/http/cookies.py
./usr/local/lib/python3.12/site-packages/future/moves/http/cookies.py
./usr/local/lib/python3.12/site-packages/pip/_vendor/requests/cookies.py

I'm not sure but I think the only thing(s) that would cause a reversion is an HA update & possibly a redownload of the Alexa Media Player integration via HACS.

Have you re-replaced that cookies.py file?

wxman60 commented 3 days ago

I hadn't replace the cookie file yet. Was waiting to hear back from you. Replaced it this morning and ran command and this is what I got back.

find . -name cookies.py -print ./usr/lib/python3.12/site-packages/pip/_vendor/requests/cookies.py ./usr/lib/python3.12/http/cookies.py find: ./proc/487/task/487/fdinfo: Permission denied find: ./proc/487/fdinfo: Permission denied find: ./proc/488/task/488/fdinfo: Permission denied find: ./proc/488/fdinfo: Permission denied

Alexa media play failed on second restart.

Menz01 commented 3 days ago

So for those of us that chose to rollback, with the upcoming 2024.7.0 coming today, should we not upgrade?

On Wed, Jul 3, 2024, 12:28 PM wxman60 @.***> wrote:

I hadn't replace the cookie file yet. Was waiting to hear back from you. Replaced it this morning and ran command and this is what I got back.

find . -name cookies.py -print ./usr/lib/python3.12/site-packages/pip/_vendor/requests/cookies.py ./usr/lib/python3.12/http/cookies.py find: ./proc/487/task/487/fdinfo: Permission denied find: ./proc/487/fdinfo: Permission denied find: ./proc/488/task/488/fdinfo: Permission denied find: ./proc/488/fdinfo: Permission denied

Alexa media play failed on second restart.

— Reply to this email directly, view it on GitHub https://github.com/alandtse/alexa_media_player/issues/2288#issuecomment-2206751033, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHOXQZ4EYEV7GXALL2ZKVLTZKQRCJAVCNFSM6AAAAABJN7M7RKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBWG42TCMBTGM . You are receiving this because you commented.Message ID: @.***>

danielbrunt57 commented 3 days ago

So for those of us that chose to rollback, with the upcoming 2024.7.0 coming today, should we not upgrade?

I won't know until I get 2024.7.0 and try with the original cookies.py file. There's been zero input from @alandtse on this. Plus I don't understand why there's a partitioned cookie with >= 2024.6.3 but not 2024.6.2.....

danielbrunt57 commented 3 days ago

Alexa media play failed on second restart.

Is your cookies.py file still the newer one??

danielbrunt57 commented 3 days ago

I just got 2024.7.0 and it wiped my updated cookies.py file and AMP failed to initialize...

Logger: homeassistant.config_entries
Source: config_entries.py:586
First occurred: 10:39:57 AM (1 occurrences)
Last logged: 10:39:57 AM

Error setting up entry daniel@brunt.ca - amazon.com for alexa_media
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 586, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/alexa_media/__init__.py", line 335, in async_setup_entry
    await login.login(cookies=await login.load_cookie())
                              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/alexapy/alexalogin.py", line 328, in load_cookie
    cookies = pickle.loads(await myfile.read())
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/http/cookies.py", line 312, in __setitem__
    raise CookieError("Invalid attribute %r" % (K,))
http.cookies.CookieError: Invalid attribute 'partitioned'

Re-replacing the file...

wxman60 commented 3 days ago

Yes, it is still the new one after restart. If I do a reboot then it reverts back to the old one.

danielbrunt57 commented 3 days ago

Yes, it is still the new one after restart. If I do a reboot then it reverts back to the old one.

Are you doing a Restart Home Assistant or a Reboot system? (and I presume you are using Home Assistant Operating System aka Supervised?)

danielbrunt57 commented 3 days ago

Well, after replacing cookies.py I have now have a different behaviour...a VERY long delay on initializing AMP which eventually gives me... image

This error originated from a custom integration.

Logger: alexapy.helpers
Source: custom_components/alexa_media/__init__.py:335
integration: Alexa Media Player (documentation, issues)
First occurred: 10:58:56 AM (1 occurrences)
Last logged: 10:58:56 AM

alexalogin.login((<alexapy.alexalogin.AlexaLogin object at 0xffff5cae1b50>,), {'cookies': {}}): Timeout error occurred accessing AlexaAPI: An exception of type CancelledError occurred. Arguments: ('Global task timeout',)
Logger: homeassistant.bootstrap
Source: bootstrap.py:1042
First occurred: 10:59:12 AM (1 occurrences)
Last logged: 10:59:12 AM
Setup timed out for stage 2 waiting on {<Task pending name='listen alexa_media_relogin_success' coro=<async_setup_entry.<locals>.login_success() running at /config/custom_components/alexa_media/__init__.py:243> wait_for=<_GatheringFuture pending cb=[Task.task_wakeup()]> cb=[set.remove()]>} - moving forward
Logger: alexapy.helpers
Source: runner.py:190
First occurred: 10:59:13 AM (1 occurrences)
Last logged: 10:59:13 AM

alexaapi.get_devices((<alexapy.alexalogin.AlexaLogin object at 0xffff5cae1b50>,), {}): A connection error occurred: An exception of type KeyError occurred. Arguments: ('daniel@brunt.ca',)
wxman60 commented 3 days ago

I did both, first a restart and the file remained the new one and then a reboot and it reverted back to the old one.

danielbrunt57 commented 3 days ago

I did both, first a restart and the file remained the new one and then a reboot and it reverted back to the old one.

I dunno...

I just removed the AMP config & re-added it and now I get an Amazon captcha which I complete but it then gives me,.,.

image

and I can't add it.

I would say this integration unfortunately is now very broken..............

wxman60 commented 3 days ago

I just received 2024.7 and going to upgrade to that. I already have an automation that reloads AMP after restart. Will just wait to see if @alandtse can find a fix for it.

wxman60 commented 3 days ago

Thanks for making the attempt to find a work around.

danielbrunt57 commented 3 days ago

I just switched from Edge to Chrome, logged into HA and was successfully able to reinstall the AMP integration so something is messed up in Edge. ...and someone posted a while back that the browser is not involved in this!

danielbrunt57 commented 3 days ago

After checking Edge settings, an update was pending and Edge restarted... image

I removed and re-added the integration using Edge browser and it successfully completed without that captcha screen. However, I restarted HA and my cookies.py file had reverted to the original and AMP failed to initialize. After re-replacing it, all is well again.

danielbrunt57 commented 3 days ago

I just received 2024.7 and going to upgrade to that. I already have an automation that reloads AMP after restart. Will just wait to see if @alandtse can find a fix for it.

You may be waiting until 2024.11 or 2024.12 as October is the scheduled release of the next python version which will hopefully address this new partitioned cookie structure, unless @alantse has time to devise and rewrite it to not use the current cookie method. That or someone figures out why HA sees it at restart while an integration reload after-the-fact does not...

wxman60 commented 3 days ago

Came across this earlier today. Looks like AMP won't be updated for a while or if ever. https://community.home-assistant.io/t/alexa-media-player-custom-integration-on-life-support-help-wanted/725684

Petro31 commented 2 days ago

Came across this earlier today. Looks like AMP won't be updated for a while or if ever.

https://community.home-assistant.io/t/alexa-media-player-custom-integration-on-life-support-help-wanted/725684

Alan is still publishing fixes provided by other developers. So this integration is in maintenance mode. Myself and a few others have been pushing various minor fixes to the integration. He is not actively developing this integration or providing fixes authored by him.

Menz01 commented 2 days ago

i am no programmer or developer, i am just a dumb user that can follow guides pretty well. so i guess what i don't get is that it works just fine on 2024.6.2 so what is causing it to break on newer versions?

wxman60 commented 2 days ago

An update from Alan just came across 4.10.3. It's a fix for "warning for async_forward_entry_setup". I just tried it and restarted a few time and Alexa Media Player is still not loading on restart. Hopefully he will address it soon.

Petro31 commented 2 days ago

An update from Alan just came across 4.10.3. It's a fix for "warning for async_forward_entry_setup". I just tried it and restarted a few time and Alexa Media Player is still not loading on restart. Hopefully he will address it soon.

Yes, I made that fix and that change wasn't going to fix this issue. It was to fix a different issue.

Menz01 commented 1 day ago

so today i saw that the Alexa Media Player integration had an update. i installed it but i am still on HA 2024.6.2 and have not upgraded to 2024.7.0 yet. anyone on 2024.7.0 and did the AMP upgrade as well? can you confirm if the issue has gone away now?