BottlecapDave / HomeAssistant-OctopusEnergy

Unofficial Home Assistant integration for interacting with Octopus Energy
https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/
MIT License
612 stars 61 forks source link

Unable to connect #663

Closed owenashurst closed 10 months ago

owenashurst commented 10 months ago

Describe the bug

Upon upgrading to the latest version and after numerous restarts, I keep getting an error about unable to connect with a timeout error. Interestingly, the Gas stats seem to work. It's just for Electric.

Reproduction steps

Upgrade from an older Octopus version to the latest.

Expected behaviour

I expect it to work as before.

Tariff Code

Unknown

Integration Version

Latest

Home Assistant Version

Latest

Fresh Install?

After upgrading

Home Assistant Logs

2023-12-31 19:39:05.705 WARNING (MainThread) [custom_components.octopus_energy.api_client] Failed to connect. Timeout of ClientTimeout(total=15, connect=None, sock_read=None, sock_connect=None, ceil_threshold=5) exceeded.
2023-12-31 19:39:05.706 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry A-0000C00E for octopus_energy
Traceback (most recent call last):
  File "/config/custom_components/octopus_energy/api_client/__init__.py", line 393, in async_refresh_token
    async with client.post(url, json=payload) as token_response:
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 1187, in __aenter__
    self._resp = await self._coro
                 ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 601, in _request
    await resp.start(conn)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 960, in start
    with self._timer:
  File "/usr/local/lib/python3.11/site-packages/aiohttp/helpers.py", line 735, in __exit__
    raise asyncio.TimeoutError from None
TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 402, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/octopus_energy/__init__.py", line 83, in async_setup_entry
    await async_setup_dependencies(hass, config)
  File "/config/custom_components/octopus_energy/__init__.py", line 128, in async_setup_dependencies
    account_info = await client.async_get_account(config[CONFIG_MAIN_ACCOUNT_ID])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/octopus_energy/api_client/__init__.py", line 411, in async_get_account
    await self.async_refresh_token()
  File "/config/custom_components/octopus_energy/api_client/__init__.py", line 407, in async_refresh_token
    raise TimeoutException()
custom_components.octopus_energy.api_client.TimeoutException 

Also I get the two lines:

2023-12-31 20:01:38.636 WARNING (MainThread) [custom_components.octopus_energy.coordinators.intelligent_dispatches] Failed to retrieve new dispatches. Next attempt at 2023-12-31 20:02:38.635948+00:00
2023-12-31 20:01:38.638 WARNING (MainThread) [custom_components.octopus_energy.coordinators.intelligent_settings] Failed to retrieve new intelligent settings. Next attempt at 2023-12-31 20:02:38.637731+00:00

Confirmation

Welshman47 commented 10 months ago

Im also getting no connection to octopus energy/home mini, it keeps saying failed to set-up.

Numerous restarts and reloads, but nothing. Octopus live app is working and receiving data from my home mini

Welshman47 commented 10 months ago

Ive tried re-installing the intergration and nothing works. I have no access to my octopus data at all.

owenashurst commented 10 months ago

I've even tried rolling back HA Core as I updated to the latest version of that too. Still the same issue.

theneiljohnson commented 10 months ago

I was seeing the same issue, but it seems to be working now? I wonder if a problem on the Octopus side?

Welshman47 commented 10 months ago

Still nothing for me……i sit in hope

owenashurst commented 10 months ago

I was seeing the same issue, but it seems to be working now? I wonder if a problem on the Octopus side?

@theneiljohnson It has to be, I've even did a downgrade to 9.0.2 and still no result.

owenashurst commented 10 months ago

If I call the API in postman for total consumption, it comes back fine. Even if i call it a couple times in quick succession, so I doubt it's a throttling issue.

theneiljohnson commented 10 months ago

Hmm, think i spoke to soon. I flicked debugging on to grab logs and then it started ok. I then switched debugging off and it started fine again. Ran for about 15 minutes and now its failed again.

owenashurst commented 10 months ago

Hmm, think i spoke to soon. I flicked debugging on to grab logs and then it started ok. I then switched debugging off and it started fine again. Ran for about 15 minutes and now its failed again.

@theneiljohnson does your logs contain a similar error message to mine?

theneiljohnson commented 10 months ago

Yes, pretty similar:


  File "/config/custom_components/octopus_energy/api_client/__init__.py", line 393, in async_refresh_token
    async with client.post(url, json=payload) as token_response:
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 1187, in __aenter__
    self._resp = await self._coro
                 ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 601, in _request
    await resp.start(conn)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 960, in start
    with self._timer:
  File "/usr/local/lib/python3.11/site-packages/aiohttp/helpers.py", line 735, in __exit__
    raise asyncio.TimeoutError from None
TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 402, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/octopus_energy/__init__.py", line 83, in async_setup_entry
    await async_setup_dependencies(hass, config)
  File "/config/custom_components/octopus_energy/__init__.py", line 128, in async_setup_dependencies
    account_info = await client.async_get_account(config[CONFIG_MAIN_ACCOUNT_ID])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/octopus_energy/api_client/__init__.py", line 411, in async_get_account
    await self.async_refresh_token()
  File "/config/custom_components/octopus_energy/api_client/__init__.py", line 407, in async_refresh_token
    raise TimeoutException()
custom_components.octopus_energy.api_client.TimeoutException

Debug logs:
2024-01-01 01:31:07.443 WARNING (MainThread) [custom_components.octopus_energy.api_client] Failed to connect. Timeout of ClientTimeout(total=15, connect=None, sock_read=None, sock_connect=None, ceil_threshold=5) exceeded.
2024-01-01 01:31:07.444 WARNING (MainThread) [custom_components.octopus_energy.coordinators.electricity_rates] Failed to retrieve new electricity rates for 1160000560708/22J0045738 - using cached rates. Next attempt at 2024-01-01 01:30:51.913366+00:00
2024-01-01 02:00:59.444 WARNING (MainThread) [custom_components.octopus_energy.api_client] Failed to connect. Timeout of ClientTimeout(total=15, connect=None, sock_read=None, sock_connect=None, ceil_threshold=5) exceeded.
2024-01-01 02:00:59.444 WARNING (MainThread) [custom_components.octopus_energy.coordinators.intelligent_settings] Failed to retrieve new intelligent settings - using cached settings. Next attempt at 2024-01-01 02:00:43.536510+00:00
owenashurst commented 10 months ago

@theneiljohnson check the logs with debugging enabled and reload the integration entity thats your account number.

If you check the JSON returned for the account, I've noticed that my meters array is empty. After checking the Octopus app, I can't see any Electricity graphs which is extremely weird.

The log should look something like this:

[custom_components.octopus_energy.api_client] account:
theneiljohnson commented 10 months ago

I'm seeing similar in the app, I can see import usage for today but export and everything is is blank. My Home Mini data is coming through but inconsistently, seems to work fine for 5 minutes or so then reports that it's no longer being reported, then comes back again without me doing anything.

My gut feeling is that there is an issue on the Octopus side, I'd imagine some young project manager somewhere thought that making system upgrades on New Years Day was a super idea :-)

owenashurst commented 10 months ago

@theneiljohnson Yeah my home mini isn't coming through at all.

I've sent them an email - phone lines are unsurprisingly closed today.

I suspect something has broken, I noticed my stats hasn't been showing in the app for a few days. Extremely weird. I guess all I can do is wait to see what they say.

theneiljohnson commented 10 months ago

If it helps, i've just created this automation to try and restart the integration if it's unavailable. I initially did it based on the state change, but then it doesn't retry later if it fails on first restart. This checks the integration state every 15 minutes and reloads if req'd.


description: Force restart of Octopus Integration if it becomes unavailable.
trigger:
  - alias: Check Octopus Integration State every 15 minutes
    platform: time_pattern
    minutes: /15
condition:
  - condition: state
    entity_id: binary_sensor.octopus_energy_a_cXXXXXX_intelligent_dispatching
    state: unavailable
    alias: Check to see if Intelligent Dispatching is Unavailable
action:
  - service: homeassistant.reload_config_entry
    target:
      entity_id: binary_sensor.octopus_energy_a_cXXXXXX_intelligent_dispatching
    data: {}
    alias: Reload Octopus Integration if Intelligent Dispatching is Unavailable
mode: restart
owenashurst commented 10 months ago

If it helps, i've just created this automation to try and restart the integration if it's unavailable. I initially did it based on the state change, but then it doesn't retry later if it fails on first restart. This checks the integration state every 15 minutes and reloads if req'd.

description: Force restart of Octopus Integration if it becomes unavailable.
trigger:
  - alias: Check Octopus Integration State every 15 minutes
    platform: time_pattern
    minutes: /15
condition:
  - condition: state
    entity_id: binary_sensor.octopus_energy_a_cXXXXXX_intelligent_dispatching
    state: unavailable
    alias: Check to see if Intelligent Dispatching is Unavailable
action:
  - service: homeassistant.reload_config_entry
    target:
      entity_id: binary_sensor.octopus_energy_a_cXXXXXX_intelligent_dispatching
    data: {}
    alias: Reload Octopus Integration if Intelligent Dispatching is Unavailable
mode: restart

Awesome thank you for that!

BottlecapDave commented 10 months ago

Hello. Sorry for the late response. Timeout exceptions unfortunately are something that can't be fixed on my end other than increasing the timeout, but this doesn't guarantee success and results in HA reporting slow integration warnings which I then have issues raised against too (lose lose situation :))

The integration uses a variety of octopus APIs, both rest and graphql (the latter being more fully fledged) and I'm assuming they're using a microservice architecture. This means that one of the API commands can be functional while other parts aren't.

Based on reports in the OE forums it looks like other people had various API issues.

A future update will reduce the hit on the API during startup, which will hopefully help make it more stable.

Welshman47 commented 10 months ago

My mini still wont connect to HA. All connected to the router and octo app no problems. But gets an unknown error when trying to connect to HA..

API is all working ok for other apps im using with octopus to.

BottlecapDave commented 10 months ago

Do you have any errors in your HA logs that might point at the issue (full details please)? If you're using the app and other apps and this integration to access your mini you might be hitting rate limits on the API and you'll need to adjust the frequency of the update in the integration.

Welshman47 commented 10 months ago

I tried reducing the refresh rate, but it just wont even load into HA at all. It just says unknown error.

At the moment its not even intergrating into HA

BottlecapDave commented 10 months ago

Can you please submit your full HA logs or screenshots of the error

Welshman47 commented 10 months ago

As i said its not logging any error…….this is the only error i see, dont worry about api key i have regenrated a new one. IMG_0475

BottlecapDave commented 10 months ago

Have you updated to the latest version of the integration? Even with this unknown error I would expect some errors or warnings in the HA logs.

Failing that, if you could set the logs of the integration to debug level (see below), try again and then look in your full logs for [custom_components.octopus_energy.api_client] account:

# Example configuration.yaml entry
logger:
  default: warning
  logs:
    custom_components.octopus_energy: debug
Welshman47 commented 10 months ago

Im new to all this dave and dont know were to paste that code…..i dont have file server intergrated yet.

Sorry

BottlecapDave commented 10 months ago

You would need to add it to your configuraion.yaml file. This can be done via standard HA setup if you install the File editor add-on. Once done, you need to restart your HA instance for the logging to take effect.

Alternatively, you can run the logging service in developer tools -> services and paste the following in yaml mode and execute the service which should set your log level. You shouldn't need to restart your HA instance this way.

service: logger.set_level
data:
  custom_components.octopus_energy: debug
Welshman47 commented 10 months ago

Ok…i will get that done over the next few days and get back to you.

Thanks

Welshman47 commented 10 months ago

After numerous HA updates and a few octopus updates, its now all working again.

owenashurst commented 10 months ago

Sorry for my late response - I contacted Octopus after I realised my Smart Meter magically disappeared from my account. After they re-added it, I was able to see my statistics again like normal. I can only imagine it was because the MPAN wasn't there, so the plugin had no way of requesting information for it.

I guess everyone here has it working again one way or another, so I'll close this issue now since it seems resolved. Thanks everyone for the help!