twrecked / hass-virtual

Virtual Components for Home Assistant
GNU General Public License v3.0
162 stars 27 forks source link

0.9.0.alpha.6 - swtich's entity gets re-created after restart #89

Open scabana opened 9 months ago

scabana commented 9 months ago

Each time home assistant is restarted, each virtual switch gets re-created:

image

homeassistant@raspberrypi:~/.homeassistant $ cat virtual.yaml
version: 1
devices:
  Ender-3 Pro:
  - platform: switch
    name: ender_3_pro
  Lumiere Ender-3 Pro:
  - platform: switch
    name: lumiere_ender_3_pro
2024-01-03 15:01:28.793 DEBUG (MainThread) [custom_components.virtual] async setup {'group_name': 'imported', 'file_name': '/home/homeassistant/.homeassistant/virtual.yaml'}
2024-01-03 15:01:28.793 DEBUG (MainThread) [custom_components.virtual] creating new cfg
2024-01-03 15:01:28.793 DEBUG (MainThread) [custom_components.virtual.cfg] no meta data yet [Errno 2] No such file or directory: '/config/.storage/virtual.meta.json'
2024-01-03 15:01:28.796 DEBUG (MainThread) [custom_components.virtual.cfg] loaded-meta-data={}
2024-01-03 15:01:28.797 DEBUG (MainThread) [custom_components.virtual.cfg] loaded-devices={'Ender-3 Pro': [{'platform': 'switch', 'name': 'ender_3_pro'}], 'Lumiere Ender-3 Pro': [{'platform': 'switch', 'name': 'lumiere_ender_3_pro'}]}
2024-01-03 15:01:28.797 DEBUG (MainThread) [custom_components.virtual.cfg] creating ender_3_pro
2024-01-03 15:01:28.797 DEBUG (MainThread) [custom_components.virtual.cfg] added entity switch/{'name': 'ender_3_pro', 'entity_id': 'switch.ender_3_pro', 'unique_id': '57e03000-d7c2-44e0-a659-009639f3d433.virtual', 'device_id': 'Ender-3 Pro'}
2024-01-03 15:01:28.797 DEBUG (MainThread) [custom_components.virtual.cfg] creating lumiere_ender_3_pro
2024-01-03 15:01:28.798 DEBUG (MainThread) [custom_components.virtual.cfg] added entity switch/{'name': 'lumiere_ender_3_pro', 'entity_id': 'switch.lumiere_ender_3_pro', 'unique_id': '75da30cd-b17d-4de6-ba37-c5c0239c9654.virtual', 'device_id': 'Lumiere Ender-3 Pro'}
2024-01-03 15:01:28.798 DEBUG (MainThread) [custom_components.virtual.cfg] no meta data yet [Errno 2] No such file or directory: '/config/.storage/virtual.meta.json'
2024-01-03 15:01:28.798 DEBUG (MainThread) [custom_components.virtual.cfg] meta before {}
2024-01-03 15:01:28.798 DEBUG (MainThread) [custom_components.virtual.cfg] meta after {'imported': {'ender_3_pro': {'unique_id': '57e03000-d7c2-44e0-a659-009639f3d433.virtual', 'entity_id': 'switch.ender_3_pro'}, 'lumiere_ender_3_pro': {'unique_id': '75da30cd-b17d-4de6-ba37-c5c0239c9654.virtual', 'entity_id': 'switch.lumiere_ender_3_pro'}}}
2024-01-03 15:01:28.798 DEBUG (MainThread) [custom_components.virtual.cfg] couldn't save meta data [Errno 2] No such file or directory: '/config/.storage/virtual.meta.json'
2024-01-03 15:01:28.799 DEBUG (MainThread) [custom_components.virtual.cfg] meta-data={'ender_3_pro': {'unique_id': '57e03000-d7c2-44e0-a659-009639f3d433.virtual', 'entity_id': 'switch.ender_3_pro'}, 'lumiere_ender_3_pro': {'unique_id': '75da30cd-b17d-4de6-ba37-c5c0239c9654.virtual', 'entity_id': 'switch.lumiere_ender_3_pro'}}
2024-01-03 15:01:28.799 DEBUG (MainThread) [custom_components.virtual.cfg] devices=[{'device_id': 'Ender-3 Pro', 'name': 'Ender-3 Pro'}, {'device_id': 'Lumiere Ender-3 Pro', 'name': 'Lumiere Ender-3 Pro'}]
2024-01-03 15:01:28.799 DEBUG (MainThread) [custom_components.virtual.cfg] entities={'switch': [{'name': 'ender_3_pro', 'entity_id': 'switch.ender_3_pro', 'unique_id': '57e03000-d7c2-44e0-a659-009639f3d433.virtual', 'device_id': 'Ender-3 Pro'}, {'name': 'lumiere_ender_3_pro', 'entity_id': 'switch.lumiere_ender_3_pro', 'unique_id': '75da30cd-b17d-4de6-ba37-c5c0239c9654.virtual', 'device_id': 'Lumiere Ender-3 Pro'}]}
2024-01-03 15:01:28.799 DEBUG (MainThread) [custom_components.virtual.cfg] orphaned-entities={}
2024-01-03 15:01:28.799 DEBUG (MainThread) [custom_components.virtual] creating the devices
2024-01-03 15:01:28.799 DEBUG (MainThread) [custom_components.virtual] creating-device={'device_id': 'Ender-3 Pro', 'name': 'Ender-3 Pro'}
2024-01-03 15:01:28.799 DEBUG (MainThread) [custom_components.virtual] creating-device={'device_id': 'Lumiere Ender-3 Pro', 'name': 'Lumiere Ender-3 Pro'}
2024-01-03 15:01:28.800 DEBUG (MainThread) [custom_components.virtual] update hass data {'imported': {'entities': {'switch': [{'name': 'ender_3_pro', 'entity_id': 'switch.ender_3_pro', 'unique_id': '57e03000-d7c2-44e0-a659-009639f3d433.virtual', 'device_id': 'Ender-3 Pro'}, {'name': 'lumiere_ender_3_pro', 'entity_id': 'switch.lumiere_ender_3_pro', 'unique_id': '75da30cd-b17d-4de6-ba37-c5c0239c9654.virtual', 'device_id': 'Lumiere Ender-3 Pro'}]}, 'file_name': '/home/homeassistant/.homeassistant/virtual.yaml'}}
2024-01-03 15:01:32.630 INFO (MainThread) [homeassistant.components.switch] Setting up switch.virtual
2024-01-03 15:01:32.631 INFO (MainThread) [homeassistant.components.device_tracker] Setting up device_tracker.virtual
2024-01-03 15:01:32.632 DEBUG (MainThread) [custom_components.virtual.switch] setting up the entries...
2024-01-03 15:01:32.632 DEBUG (MainThread) [custom_components.virtual.entity] creating-virtual-switch={'name': 'ender_3_pro', 'entity_id': 'switch.ender_3_pro', 'unique_id': '57e03000-d7c2-44e0-a659-009639f3d433.virtual', 'device_id': 'Ender-3 Pro', 'initial_value': 'off', 'persistent': True, 'initial_availability': True}
2024-01-03 15:01:32.632 DEBUG (MainThread) [custom_components.virtual.entity] setting up device info
2024-01-03 15:01:32.632 INFO (MainThread) [custom_components.virtual.entity] VirtualEntity ender_3_pro created
2024-01-03 15:01:32.632 INFO (MainThread) [custom_components.virtual.switch] VirtualSwitch: ender_3_pro created
2024-01-03 15:01:32.632 DEBUG (MainThread) [custom_components.virtual.entity] creating-virtual-switch={'name': 'lumiere_ender_3_pro', 'entity_id': 'switch.lumiere_ender_3_pro', 'unique_id': '75da30cd-b17d-4de6-ba37-c5c0239c9654.virtual', 'device_id': 'Lumiere Ender-3 Pro', 'initial_value': 'off', 'persistent': True, 'initial_availability': True}
2024-01-03 15:01:32.632 DEBUG (MainThread) [custom_components.virtual.entity] setting up device info
2024-01-03 15:01:32.632 INFO (MainThread) [custom_components.virtual.entity] VirtualEntity lumiere_ender_3_pro created
2024-01-03 15:01:32.632 INFO (MainThread) [custom_components.virtual.switch] VirtualSwitch: lumiere_ender_3_pro created
2024-01-03 15:01:32.635 INFO (MainThread) [homeassistant.components.todo] Setting up todo.shopping_list
2024-01-03 15:01:32.636 DEBUG (MainThread) [custom_components.virtual.device_tracker] setting up the device_tracker entries...
2024-01-03 15:01:32.636 DEBUG (MainThread) [custom_components.virtual.device_tracker] installing handlers
2024-01-03 15:01:32.638 INFO (MainThread) [homeassistant.components.tts] Setting up tts.google_translate
2024-01-03 15:01:32.639 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.virtual entity: switch.ender_3_pro_3
2024-01-03 15:01:32.639 INFO (MainThread) [custom_components.virtual.entity] VirtualEntity 57e03000-d7c2-44e0-a659-009639f3d433.virtual: creating initial state
2024-01-03 15:01:32.640 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.virtual entity: switch.lumiere_ender_3_pro_3
2024-01-03 15:01:32.640 INFO (MainThread) [custom_components.virtual.entity] VirtualEntity 75da30cd-b17d-4de6-ba37-c5c0239c9654.virtual: creating initial state

While writing the bug, I saw this: 2024-01-03 15:01:28.798 DEBUG (MainThread) [custom_components.virtual.cfg] no meta data yet [Errno 2] No such file or directory: '/config/.storage/virtual.meta.json', took a look at the code and the path is hard-coded here. When installing Home Assistant using this setup, the .storage folder ends up in /home/homeassistant/.homeassistant/.storage not at the root. I added the necessary folder and file /config/.storage/virtual.meta.json and now the entities don't get re-created. Seems like HACS automatically saves it to the right location using this store.

twrecked commented 8 months ago

Sorry about this, I was on holiday and missed this one. It should be fixed now, can you try the latest build it should use the correct directory.