BottlecapDave / HomeAssistant-OctopusEnergy

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

Error setting up entry A-<account ID> for octopus_energy - "KeyError: 'account_id'" #923

Closed alexhk90 closed 1 week ago

alexhk90 commented 1 week ago

Describe the bug

Error showing up on the integration page under "Needs attention" - "Failed to set up: Check the logs", however integration appears to actually be working as usual (new rates for Agile tariff updating each day, target rate sensors working). See "Home Assistant Logs" for details (with debug logging enabled).

Reproduction steps

Restart Home Assistant or reload the integration.

Expected behaviour

Not to have this error message in the logs / integration page.

Tariff Code

E-1R-AGILE-23-12-06-C

Integration Version

11.2.0

Home Assistant Version

2024.6.3

Fresh Install?

After upgrading

Home Assistant Logs

Logger: homeassistant.config_entries
Source: config_entries.py:594
First occurred: June 16, 2024 at 6:24:01 PM (3 occurrences)
Last logged: 12:17:42 AM
Error setting up entry A-<account ID> for octopus_energy

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/octopus_energy/__init__.py", line 147, in async_setup_entry
    if child_entry.data[CONFIG_KIND] != CONFIG_KIND_ACCOUNT and child_entry.data[CONFIG_ACCOUNT_ID] == account_id:
                                                                ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'account_id'

Logger: homeassistant.config_entries
Source: config_entries.py:594
First occurred: 12:06:36 AM (14 occurrences)
Last logged: 12:17:42 AM

    Error setting up entry A-<account ID> for text
    Error setting up entry A-<account ID> for number
    Error setting up entry A-<account ID> for switch
    Error setting up entry A-<account ID> for time
    Error setting up entry A-<account ID> for event

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 "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 134, in async_setup_entry
    return await component.async_setup_entry(entry)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 185, in async_setup_entry
    raise ValueError(
ValueError: Config entry A-<account ID> (<ID/hash>) for octopus_energy.sensor has already been setup!

Confirmation

BottlecapDave commented 1 week ago

Hello. Sorry that you're seeing this issue. May I ask what version you upgraded from? All config entries should include the account id field (which was migrated a few versions ago).

alexhk90 commented 1 week ago

Do you know if there is a way of checking the version history of this integration in Home Assistant? I tend to upgrade whenever I notice a new version in HACS, but don't know the exact version numbers unfortunately. I think this error has been showing for a few weeks now, so probably through at least at couple of version upgrades, which would be consistent what what you mention about a change a few versions ago.

BottlecapDave commented 1 week ago

There is none to my knowledge. If you feel comfortable it would be good to get a redacted look at all config entries that are stored within . storage/config_entries (I believe this is the name of the file) that have a domain of octopus_energy.

alexhk90 commented 1 week ago

Sure - attached: octopus_energy.core.config_entries.txt

Looks like perhaps a couple of my target rate sensors (now disabled/unused) were set up before the "account_id" field was added, so maybe I can just delete / re-create these to resolve the error message in the log?

BottlecapDave commented 1 week ago

Interesting. I would hope that HA doesn't only migrate non disabled config entries but that's what it looks like. If you delete those entries, then this should fix your issue, as you suggested.

alexhk90 commented 1 week ago

Confirmed that deleting the disabled target rate sensor (that didn't have the "account_id" field) via the Octopus Energy integration page and reloading the integration (on the same page) worked to clear the error message - thank you!