Skip to content

[🐛Bug]: Deleting device leaves orphaned entities #185

@TCWORLD

Description

@TCWORLD

Describe the bug

When deleting a device (or the integration), errors occur while removing each sensor entity (one of the following for each):

Image

The net result of this is that orphaned entities are left behind in home assistant which cannot be removed. Manually deleting them is not possible because they have no unique ID:

Image

The orphans survive rebooting home assistant, and even survive the "Spook" integrations "Delete all orphaned entities", so has basically messed up the home assistant database and from searches the only way to remove them now is to manually edit the database and risk completely bricking home assistant.

To Reproduce
Steps to reproduce the behavior:

  1. Add a dynamic energy cost device.
  2. Delete it

Expected behavior
All entities associated with the device should be cleanly removed from home assistant.

Screenshots/screen recording
If applicable, add screenshots and/or screen recording to help explain your problem.

Home Assistant installation type

  • Home Assistant OS
  • Home Assistant Supervised
  • Home Assistant Core

Dynamic Energy Cost Version
Version you use:

  • Version v0.9.0

You can find the latest version here.

Additional context
Error message copyable text.

2025-09-03 22:44:06.229 ERROR (MainThread) [homeassistant.components.sensor] Error while removing entity sensor.todays_self_consumption_hourly_energy_cost
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1401, in async_remove
    await self.__async_remove_impl(force_remove)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1417, in __async_remove_impl
    await self.async_will_remove_from_hass()
  File "/config/custom_components/dynamic_energy_cost/entity.py", line 131, in async_will_remove_from_hass
    await self.hass.async_add_executor_job(self.event_unsub())
  File "/usr/local/lib/python3.13/concurrent/futures/thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 1020, in async_reset
    await entity.async_remove()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1406, in async_remove
    self.__remove_future.set_result(None)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
asyncio.exceptions.InvalidStateError: invalid state

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions