alandtse/alexa_media_player

Added 2nd Alexa Account: update_last_called is very slow

Closed this issue · 17 comments

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

I added a second Alexa account to my Alexa Media Player integration and found out that updating the last_called sensor is very slow afterwards. It takes several minutes. I tried many things including adding the email data field to the update_last_called service and deactivating the second account. After deactivating the second account the speeds goes back to 1-3 seconds for updating the last_called sensor. Also reverting the Alexa Media Player integration to another version doesn't help.

To Reproduce

  1. Add a second alexa account.
  2. Run the update_last_called service.
  3. Check the last_called sensor.
  4. Run a command on another alexa device.
  5. Run the update_last_called service.
  6. Check the last_called sensor.
  7. See that it will not change unless you wait several minutes.

Expected behavior

I expected to be able to add more than one account without getting this kind of latency. All devices are on the same network and have a great wireless connection.

Screenshots

No screenshots until now.

System details

  • Home-assistant (version): 2024.5.5
  • alexa_media (version from const.py or HA startup): 4.10.1
  • alexapy (version from pip show alexapy or HA startup): 1.27.10
  • Amazon 2FA is enabled (y/n). <!---We will not debug login issues if unanswered--->: y

Logs
I dont know if there are any sensible information in the debug logs of the integration.

Additional context

No

Never had this issue, but not going to create a second account to test it. Let's see if anyone else does and wants to fix it.

I don't have a second account but I am facing this issue too for a long time.
In sometime in the past I had to remove Alexa account configuration and reconfigure it.

This bug report has been labelled as help wanted since there has been no activity in the last 3 weeks. It will not be closed automatically.

@JensRudolph Could you describe for me your use case for needing two alexa accounts configured? I'm trying to fathom what it might be and I'm unable to...

@danielbrunt57 I am using one account for myself and one is for my parents.
We live in separate houses, while the HA server controlling both smart homes is in my house.
So in order to get use of the Alexa media player I need to add two accounts. We don't want to combine all in one account.

This bug report has been labelled as help wanted since there has been no activity in the last 3 weeks. It will not be closed automatically.

7. See that it will not change unless you wait several minutes.

I think the service alexa_media.update_last_called might only be referencing the primary account and you then have to wait for the automatic poll of both accounts to occur. If my hunch is correct, I'm not sure how to rewrite that code to have it update both primary and secondary accounts.

    async def last_call_handler(self, call):
        """Handle last call service request.

        Args
        call: List of case-sensitive Alexa email addresses. If None
                            all accounts are updated.

        """
        requested_emails = call.data.get(ATTR_EMAIL)
        _LOGGER.debug("Service update_last_called for: %s", requested_emails)
        for email, account_dict in self.hass.data[DATA_ALEXAMEDIA]["accounts"].items():
            if requested_emails and email not in requested_emails:
                continue
            login_obj = account_dict["login_obj"]
            try:
                await self.functions.get("update_last_called")(login_obj)
            except AlexapyLoginError:
                report_relogin_required(self.hass, login_obj, email)
            except AlexapyConnectionError:
                _LOGGER.error(
                    "Unable to connect to Alexa for %s;"
                    " check your network connection and try again",
                    hide_email(email),
                )

@JensRudolph
Can you check your logs for the entry generated by _LOGGER.debug("Service update_last_called for: %s", requested_emails) when you run the service call?
You can filter the full log by Service update_last_called for to find just those entries.
I'd like to know what the requested_emails value is is after the for :...

@danielbrunt57
I also tried to use the update_last_called service call with the email data field. Also then both account were lagging.
I was able to clear that lag by disabling all devices that had the same name on both accounts. For example "This device" and a group called the same.
I don't know if it's possible for you to make this work without disabling those.

So are you saying that without duplicate device names that you have no lag when calling the service with no email or only no lag when calling separately with each email?

With both service calls no lag.

Recently I once saw in one of my hundreds of restarts in the last two months a device or service entity with my email appended and thought wtf? what's that about? Fixed what I needed to fix, restarted and back to normal again but realized now it was somehow related to a phantom 2nd account that I've never tried. So that occurrence gives me food for thought. I'd rather see the integration name or id appended to those duplicate devices or at least _1 and _2. But name would be better as you can rename the config entry however you like. The rename operation would have to ensure it it unique within the parent alexa_media domain though. I do have access to my fiance's Alexa account that I could test with and it will take me quite some time to possibly figure this out but unfortunately my spare time in the next couple of months will likely be zero...

That's fine, as I said without those identical names, it works for me. If you want to fix it, you can do it, but if you haven't got the time, you can leave it as it is.
Thank you for addressing this issue.

Just to double clarify: a single service call with no email and no dups there's no lag?

If so, at the very least i can add that info to the wiki...

Note: When two accounts are configured, all Alexa device names have to be unique across both accounts when calling the service with no email account specified which tries to update the last_called from both accounts. If not, the update_last_called service will fail to complete and last_called will not update correctly until the next scheduled poll.

No, without duplicates there is no lag, also without the email data.
Thank you for adding this to the wiki.