ha-emporia-vue icon indicating copy to clipboard operation
ha-emporia-vue copied to clipboard

401 Client Error: Unauthorized for url

Open happydutch opened this issue 3 years ago • 5 comments

The integration is working (big thank you for making this!!!), however I see a lot of errors in the log:

2020-11-16 16:32:40 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.power_dishwasher_12_1min fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 278, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 471, in async_device_update
    await self.hass.async_add_executor_job(self.update)  # type: ignore
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/emporia_vue/sensor.py", line 83, in update
    channels = self._vue.get_recent_usage(scale=self._scale)
  File "/usr/local/lib/python3.8/site-packages/pyemvue/pyemvue.py", line 115, in get_recent_usage
    return self.get_usage_for_time_scale(now, scale, unit)[0]
  File "/usr/local/lib/python3.8/site-packages/pyemvue/pyemvue.py", line 126, in get_usage_for_time_scale
    response.raise_for_status()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.emporiaenergy.com/usage/devices?start=2020-11-17T00:32:39.819010Z&end=2020-11-17T00:32:40.819010Z&scale=1MIN&unit=WATTS&customerGid=XXXXX

CustomerGid removed.

happydutch avatar Nov 17 '20 01:11 happydutch

Oh, unfortunate. It looks like they restricted that api call, I see it on my end too as of about 5:10pm EST. I'll look into it and see if I can't figure something out. Hopefully they didn't lock down the API.

magico13 avatar Nov 17 '20 01:11 magico13

The odd thing is that I see entities being updated. It appears to be somehow transient.

Example of updates:

sensor.power_fridge_xbox_tv_14_1min 59 2020-11-17T00:55:12.812266+00:00 (21 minutes ago) sensor.power_fridge_xbox_tv_14_1min 134 2020-11-17T00:56:12.398558+00:00 (20 minutes ago) sensor.power_fridge_xbox_tv_14_1min 129 2020-11-17T00:57:12.571046+00:00 (19 minutes ago) sensor.power_fridge_xbox_tv_14_1min 128 2020-11-17T00:59:12.471251+00:00 (17 minutes ago) sensor.power_fridge_xbox_tv_14_1min 127 2020-11-17T01:04:12.773115+00:00 (12 minutes ago) sensor.power_fridge_xbox_tv_14_1min 205 2020-11-17T01:05:12.856296+00:00 (11 minutes ago) sensor.power_fridge_xbox_tv_14_1min 207 2020-11-17T01:06:12.856766+00:00 (10 minutes ago) sensor.power_fridge_xbox_tv_14_1min 236 2020-11-17T01:07:13.352758+00:00 (9 minutes ago) sensor.power_fridge_xbox_tv_14_1min 155 2020-11-17T01:08:12.996558+00:00 (8 minutes ago) sensor.power_fridge_xbox_tv_14_1min 140 2020-11-17T01:09:12.415667+00:00 (7 minutes ago) sensor.power_fridge_xbox_tv_14_1min 139 2020-11-17T01:10:12.375706+00:00 (6 minutes ago) sensor.power_fridge_xbox_tv_14_1min 138 2020-11-17T01:11:12.456629+00:00 (5 minutes ago) sensor.power_fridge_xbox_tv_14_1min 139 2020-11-17T01:12:12.332994+00:00 (4 minutes ago) sensor.power_fridge_xbox_tv_14_1min 151 2020-11-17T01:13:12.502515+00:00 (3 minutes ago) sensor.power_fridge_xbox_tv_14_1min 139 2020-11-17T01:14:12.476663+00:00 (2 minutes ago) sensor.power_fridge_xbox_tv_14_1min 117 2020-11-17T01:15:12.275911+00:00 (a minute ago) sensor.power_fridge_xbox_tv_14_1min 115 2020-11-17T01:16:14.064991+00:00 (17 seconds ago)

First failure:

2020-11-16 13:32:13 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.power_fridge_xbox_tv_14_1d fails

happydutch avatar Nov 17 '20 01:11 happydutch

Actually, false alarm on my end. I saw that my sensors stopped updating and assumed that's what it was but I think it's just #13 creeping up again.

If it's transient then I would expect it maybe happens an integer number of hours apart. The authorization tokens are valid for an hour so if it were to expire between calls I could see that happening. Alternatively it could be a rate limit thing. It looks like you have at least 14 channels (Emporia Vue Gen 2?) and right now this is set up to make a request for each channel, not one big request once per minute which is what I'm going to swap over to when I take care of #2. That means there could be a lot of requests in a short time which would maybe hit a rate limit and return that error.

magico13 avatar Nov 17 '20 01:11 magico13

The AWS API Gateway docs only list a 401 response when the authorizer has a problem and given that they use AWS for everything else it seems likely they're using API Gateway as well. It could just be a transient issue on their side. If you keep seeing it in the logs let me know but hopefully it's just a fluke.

magico13 avatar Nov 17 '20 01:11 magico13

Thanks for looking into it. I have 16 circuits and approx. 11 more will be added on a 2nd unit. So will be spamming them a bit...

Will keep you in the loop if I see more of this. Looking forward to see your batch mode coming online...

happydutch avatar Nov 17 '20 02:11 happydutch