bruxy70 / Garbage-Collection

🗑 Custom Home Assistant sensor for scheduling garbage collection (or other regularly re-occurring events - weekly on given days, semi-weekly or monthly)
MIT License
383 stars 90 forks source link

KeyError: 'days' when upgrading #439

Closed Morphy99 closed 1 year ago

Morphy99 commented 1 year ago

Before you submit a new bug report, please check that

Describe the bug

I've just completed doing an upgrade to my HA instance from 2021.11 to the latest. I've managed to get everything working OK apart from this integration. I get this error in the HA logs:

2022-11-20 23:03:11.673 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up garbage_collection platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 289, in _async_setup_platform
await asyncio.gather(*pending)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 692, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 788, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/garbage_collection/sensor.py", line 144, in async_added_to_hass
self._days = state.attributes[const.ATTR_DAYS]
KeyError: 'days'

I tried removing all sensors and even removing the custom component from HACS but it still reoccurred when setting up from fresh.

Configuration

{
  "home_assistant": {
    "installation_type": "Home Assistant OS",
    "version": "2022.11.3",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.10.7",
    "docker": true,
    "arch": "x86_64",
    "timezone": "Europe/London",
    "os_name": "Linux",
    "os_version": "5.15.74",
    "supervisor": "2022.10.2",
    "host_os": "Home Assistant OS 9.3",
    "docker_version": "20.10.18",
    "chassis": "embedded",
    "run_as_root": true
  },
  "custom_components": {
    "garbage_collection": {
      "version": "4.8.3",
      "requirements": [
        "python-dateutil>=2.8.2"
      ]
    },
    "google_home": {
      "version": "1.9.15",
      "requirements": [
        "glocaltokens==0.6.8"
      ]
    },
    "sun2": {
      "version": "2.1.3",
      "requirements": []
    },
    "frigate": {
      "version": "3.0.0",
      "requirements": []
    },
    "lockuser": {
      "version": "0.1.0",
      "requirements": []
    },
    "reolink_dev": {
      "version": "0.57",
      "requirements": [
        "reolink==0.0.62"
      ]
    },
    "hacs": {
      "version": "1.28.3",
      "requirements": [
        "aiogithubapi>=22.2.4"
      ]
    },
    "unified_remote": {
      "version": "6",
      "requirements": []
    },
    "tapo_control": {
      "version": "4.1.3",
      "requirements": [
        "pytapo==2.5",
        "onvif-zeep-async==1.2.0"
      ]
    }
  },
  "integration_manifest": {
    "domain": "garbage_collection",
    "name": "Garbage Collection",
    "version": "4.8.3",
    "documentation": "https://github.com/bruxy70/Garbage-Collection/",
    "issue_tracker": "https://github.com/bruxy70/Garbage-Collection/issues",
    "iot_class": "calculated",
    "integration_type": "helper",
    "dependencies": [],
    "config_flow": true,
    "codeowners": [
      "@bruxy70"
    ],
    "requirements": [
      "python-dateutil>=2.8.2"
    ],
    "is_built_in": false
  },
  "data": {
    "entity_id": "sensor.waste_black_bin",
    "state": "unavailable",
    "attributes": {
      "days": null,
      "last_collection": null,
      "last_updated": null,
      "next_date": null,
      "device_class": "garbage_collection__schedule"
    },
    "config_entry": {
      "entry_id": "64b4bb2f178b11eba11a9b9819cccfc2",
      "version": 6,
      "domain": "garbage_collection",
      "title": "Waste Black Bin",
      "data": {
        "unique_id": "99f4d75c-5903-41e0-9aae-e1ad15de290b"
      },
      "options": {
        "frequency": "weekly",
        "icon_normal": "mdi:trash-can",
        "icon_today": "mdi:delete-restore",
        "icon_tomorrow": "mdi:delete-circle",
        "first_month": "jan",
        "last_month": "dec",
        "collection_days": [
          "tue"
        ],
        "verbose_format": "on {date}, in {days} days",
        "date_format": "%d-%b-%Y",
        "verbose_state": false,
        "hidden": false,
        "manual_update": false
      },
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "unique_id": null,
      "disabled_by": null
    }
  }
}

Debug logs

2022-11-20 23:15:34.111 DEBUG (MainThread) [custom_components.garbage_collection] Setting Waste Black Bin (weekly) from ConfigFlow
2022-11-20 23:15:34.111 DEBUG (MainThread) [custom_components.garbage_collection] Setting Waste Green Bin (every-n-weeks) from ConfigFlow
2022-11-20 23:15:34.112 DEBUG (MainThread) [custom_components.garbage_collection] Setting Waste Recycling (every-n-weeks) from ConfigFlow
2022-11-20 23:15:34.337 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform garbage_collection
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 692, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 788, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/garbage_collection/sensor.py", line 144, in async_added_to_hass
self._days = state.attributes[const.ATTR_DAYS]
KeyError: 'days'
2022-11-20 23:15:34.344 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform garbage_collection
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 692, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 788, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/garbage_collection/sensor.py", line 144, in async_added_to_hass
self._days = state.attributes[const.ATTR_DAYS]
KeyError: 'days'
2022-11-20 23:15:34.347 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform garbage_collection
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 692, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 788, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/garbage_collection/sensor.py", line 144, in async_added_to_hass
self._days = state.attributes[const.ATTR_DAYS]
KeyError: 'days'
2022-11-20 23:15:34.415 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up garbage_collection platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 289, in _async_setup_platform
await asyncio.gather(*pending)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 692, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 788, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/garbage_collection/sensor.py", line 144, in async_added_to_hass
self._days = state.attributes[const.ATTR_DAYS]
KeyError: 'days'
2022-11-20 23:15:34.422 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up garbage_collection platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 289, in _async_setup_platform
await asyncio.gather(*pending)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 692, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 788, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/garbage_collection/sensor.py", line 144, in async_added_to_hass
self._days = state.attributes[const.ATTR_DAYS]
KeyError: 'days'
2022-11-20 23:15:34.429 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up garbage_collection platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 289, in _async_setup_platform
await asyncio.gather(*pending)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 692, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 788, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/garbage_collection/sensor.py", line 144, in async_added_to_hass
self._days = state.attributes[const.ATTR_DAYS]
KeyError: 'days'
bruxy70 commented 1 year ago

I've tried to reproduce it, but I do not get the error. It fails on restoring the state - it finds the stored state, but the state is missing the attribute. Not sure why. perhaps something related to the update and the condition before the update. Not sure. I suppose if you'd remove and re-create the helper, that error would disappear?

bruxy70 commented 1 year ago

Anyhow, I am adding a code to check for the individual attributes in the stored state. Said that, not sure how this could happen, so I might be just adding an extra code. Will see - it should not breath other things :)

bruxy70 commented 1 year ago

Check the latest release if it helps. If yes, please close the ticket.

Morphy99 commented 1 year ago

I suppose if you'd remove and re-create the helper, that error would disappear?

I tried this, I even removed the HACS component but I kept getting the same error. Maybe I was missing something it's been a long hard slog dragging it into 2022.11 :laughing:

Check the latest release if it helps. If yes, please close the ticket.

I've tested and it's back working, thank you so much!