HDMI CEC: "Entity id already exists" and can't control switches

Hi All,

Recently one of my automations which turns off my TV over HDMI CEC from the connected Raspberry Pi running OSMC stopped working (I think after the update to 0.63, but I can’t be sure). I only got the chance to look at this yesterday and I found that I can see the state of the switches but can’t control them (turning the switch off leaves the TV on, etc.). The problem persists in 0.64 and other than upgrading the config has stayed the same.

My HDMI CEC config is as follows:

hdmi_cec:
  host: !secret kodi_host

On start up I get the following error in my logs (actually one for each connected HDMI device):

Mar 06 21:51:42 homeassistant hass[543]: 2018-03-06 21:51:42 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Mar 06 21:51:42 homeassistant hass[543]: Traceback (most recent call last):
Mar 06 21:51:42 homeassistant hass[543]:   File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
Mar 06 21:51:42 homeassistant hass[543]:     result = coro.send(None)
Mar 06 21:51:42 homeassistant hass[543]:   File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 260, in _async_add_entity
Mar 06 21:51:42 homeassistant hass[543]:     msg)
Mar 06 21:51:42 homeassistant hass[543]: homeassistant.exceptions.HomeAssistantError: Entity id already exists: switch.hdmi_0
Mar 06 21:51:42 homeassistant hass[543]: 2018-03-06 21:51:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event system_log_event[L]: exception=Traceback (most recent call last):
Mar 06 21:51:42 homeassistant hass[543]:   File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
Mar 06 21:51:42 homeassistant hass[543]:     result = coro.send(None)
Mar 06 21:51:42 homeassistant hass[543]:   File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 260, in _async_add_entity
Mar 06 21:51:42 homeassistant hass[543]:     msg)
Mar 06 21:51:42 homeassistant hass[543]: homeassistant.exceptions.HomeAssistantError: Entity id already exists: switch.hdmi_0
Mar 06 21:51:42 homeassistant hass[543]: , timestamp=1520326302.6527023, source=helpers/entity_platform.py, level=ERROR, message=Error doing job: Task exception was never retrieved>

Anyone got any ideas what’s going on?

Could be related to the new entity registry feature. Try moving the file entity_registry.yaml to a backup place and restart HA. That should restore this file and probably solve the issue.

Same problem here.

File entity_registry.yaml removed but same issue.

I only have this in my configuration for sec :

hdmi_cec:

Yep, confirmed here too. My entity registry only contained an entry for my Chromecast, so it makes sense that removing it didn’t make a difference.

Looks like this is a bug. I’ll report it later today.

Issue posted: https://github.com/home-assistant/home-assistant/issues/12977

As a follow up to this, I’ve implemented a flow in Node-RED which works around this issue for me: https://webworxshop.com/2018/05/22/hdmi-cec-for-home-assistant-with-node-red.

I know it’s not the right way to fix, but here is what I made and it works for my usage :

homeassistant@rpi3:~$ diff -u  /srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py.old  /srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py
--- /srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py.old 2018-05-23 19:09:34.692849510 +0200
+++ /srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py     2018-05-23 19:09:59.252695151 +0200
@@ -299,6 +299,8 @@
         if not valid_entity_id(entity.entity_id):
             raise HomeAssistantError(
                 'Invalid entity id: {}'.format(entity.entity_id))
+        elif 'hdmi' in entity.entity_id:
+            msg = 'Entity id is CEC: {}'.format(entity.entity_id)
         elif entity.entity_id in component_entities:
             msg = 'Entity id already exists: {}'.format(entity.entity_id)
             if entity.unique_id is not None:
homeassistant@rpi3:~$

It actually happens because Entity.update() is called before entity creation:

--- a/homeassistant/components/hdmi_cec.py
+++ b/homeassistant/components/hdmi_cec.py
@@ -328,7 +328,6 @@ class CecDevice(Entity):
         self._state = STATE_UNKNOWN
         self._logical_address = logical
         self.entity_id = "%s.%d" % (DOMAIN, self._logical_address)
-        device.set_update_callback(self._update)

--- a/homeassistant/components/media_player/hdmi_cec.py
+++ b/homeassistant/components/media_player/hdmi_cec.py
@@ -39,7 +47,6 @@ class CecPlayerDevice(CecDevice, MediaPlayerDevice):
         CecDevice.__init__(self, hass, device, logical)
         self.entity_id = "%s.%s_%s" % (
             DOMAIN, 'hdmi', hex(self._logical_address)[2:])
-        self.update()
 
     def send_keypress(self, key):

I’ve prepared the PR for this.

1 Like