sensor.avanza_stock icon indicating copy to clipboard operation
sensor.avanza_stock copied to clipboard

Stock sensor stopped updating

Open divirg opened this issue 1 year ago • 15 comments

Sensor updating stopped working, apparently for no reason. No change in configuration for was applied. It looks like the connection is forcibly corded

Logger: homeassistant.helpers.entity
Source: helpers/entity.py:942
First occurred: November 3, 2024 at 10:11:21 AM (295 occurrences)
Last logged: 8:11:21 PM

Update for sensor.alphabet fails
Update for sensor.apple fails
Update for sensor.robo_global fails
Update for sensor.health fails
Update for sensor.world_it fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 942, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1300, in async_device_update
    await self.async_update()
  File "/config/custom_components/avanza_stock/sensor.py", line 250, in async_update
    data = await pyavanza.get_stock_async(self._session, self._stock)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyavanza/__init__.py", line 87, in get_stock_async
    return await request_url_async(session, url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyavanza/__init__.py", line 67, in request_url_async
    async with session.get(url, raise_for_status=True) as resp:
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 1355, in __aenter__
    self._resp: _RetType = await self._coro
                           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 492, in _request
    raise RuntimeError("Session is closed")
RuntimeError: Session is closed

divirg avatar Nov 05 '24 19:11 divirg

I decided to restart home assistant and that fixed the issue. Before I close the issue is there anything I could test and report in future just in case the issue pops up again?

divirg avatar Nov 05 '24 20:11 divirg

One thing to check is if the api is down, you can do that by visiting this url in your browser:

https://www.avanza.se/_api/market-guide/stock/{orderbook_id}

Change "{orderbook_id}" to the id you have for your stock.

I dont think that was the issue here, did any other cloud-based sensors of yours fail too?

Thanks for reporting :)

claha avatar Nov 06 '24 05:11 claha

No other web sensor failed in the whole HA. I'll check if it will ever happen again. Of course I can say that restarting HA fixed it immediately... Though I never like "restarting" as a solution, but since everything else was up, that was the last thing to try.

Closing the issue and thanks for the feedback.

divirg avatar Nov 06 '24 06:11 divirg

It did happen again and API was correctly responding from the browser. If you suggest anything I could check or debug I would be happy to help. Restarting made an immediate sensor update. To be clear the stock price was frozen to the day before during opening hours, no update at all, I am not talking about delays. Happy to help if I can.

divirg avatar Nov 09 '24 10:11 divirg

I guess the error is the same as before, I'll try and find out why/when "Session is closed" happens and how to handle that

claha avatar Nov 09 '24 11:11 claha

Happy to help, if you need anything just let me know.

divirg avatar Nov 09 '24 12:11 divirg

That's right for me. I installed the addin for the first time yesterday. No update since then.

RalfEs73 avatar Nov 19 '24 09:11 RalfEs73

That's right for me. I installed the addin for the first time yesterday. No update since then.

Any errors? What is your config?

claha avatar Nov 19 '24 21:11 claha

That's right for me. I installed the addin for the first time yesterday. No update since then.

Any errors? What is your config?

Same here (trying to prevent the 'same here!' only reply ;-) No config changes, only updates once after a HA restart and that's it. I turned on Debug logging which only shows one line after the restart and then goes silent:

2024-12-16 18:54:56.245 DEBUG (MainThread) [custom_components.avanza_stock.sensor] Tracking Tesla [238449] using Avanza

my config (in sensors.yaml):

- platform: avanza_stock
  stock: 238449
  name: Tesla

If I can help in providing more info or such, let me know.

cheers

roelsroels avatar Dec 16 '24 18:12 roelsroels

Same for me. Sensors didnt fetch new state for 21 Minutes now. I am tracking the last updated timesstamp (as the built-in attribute is never populated)

BlaXun avatar Jan 09 '25 12:01 BlaXun

Same for me. Sensors didnt fetch new state for 21 Minutes now. I am tracking the last updated timesstamp (as the built-in attribute is never populated)

60minutes is the default update interval, unless you configured something else. I just checked my stocks and they updated 5min ago

claha avatar Jan 09 '25 12:01 claha

Same for me. Sensors didnt fetch new state for 21 Minutes now. I am tracking the last updated timesstamp (as the built-in attribute is never populated)

60minutes is the default update interval, unless you configured something else. I just checked my stocks and they updated 5min ago

Hold on, thats configurable? I thought it was fixed at 15 minute intervalls... how did I not see that!?

Okay, found the 60 minutes in the source code. Is there any limit they are enforcing? Whats the lowest I could go?

BlaXun avatar Jan 09 '25 13:01 BlaXun

You can do

- platform: avanza_stock
  scan_interval: 60

I guess 1,1min is the lowest. This is builtin (somehow) since this is a yaml only integraton.

claha avatar Jan 09 '25 14:01 claha

Not sure if avanza could ip block you though, bit never happened what Ive heard 😃

claha avatar Jan 09 '25 14:01 claha

Interestingly I got hit with the "Session is closed" today too. If it happens again, I'll try to get some debug logs etc.

jamespreedy avatar Apr 17 '25 17:04 jamespreedy