twrecked / hass-virtual

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

0.9.x - Cannot setup #94

Closed Tony763 closed 9 months ago

Tony763 commented 9 months ago

Hi @twrecked, just started to try new 0.9.x version (current master). Following error occurred immediately at begging. Configuration is from pre-0.8.x version. Hass version 2024.1.5

Traceback (most recent call last):
  File "/srv/ha/venv/lib/python3.11/site-packages/homeassistant/setup.py", line 332, in _async_setup_component
    result = await task
             ^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.homeassistant/custom_components/virtual/__init__.py", line 68, in setup
    hass.async_create_task(
  File "/srv/ha/venv/lib/python3.11/site-packages/homeassistant/core.py", line 624, in async_create_task
    task = self.loop.create_task(target, name=name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 436, in create_task
    task = tasks.Task(coro, loop=self, name=name, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 763, in call_soon
    self._check_thread()
  File "/usr/lib/python3.11/asyncio/base_events.py", line 800, in _check_thread
    raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
Tony763 commented 9 months ago

Changing def setup(... to async def async_setup(.... allow integration to start without error. But no configuration from configuration.yaml is added. HA only complain about non supported configuration entries in platforms (light, switch,..)

twrecked commented 9 months ago

Was this seen on an upgrade?

edit: just checking the code, it had to have been...

Tony763 commented 9 months ago

Yes. Note: I use it as cloned repository so upgrade was done by git pull and ha restart.

twrecked commented 9 months ago

Thanks for the confirmation. I'll look into this.

The def setup call is just used to trigger the upgrade, subsequent starts will not get to the async_create_task code which is why I'm not seeing it here. I'll revert here and fix it.

It also explains why none of your config made it in the upgrade.

twrecked commented 9 months ago

I just merged in your fix.

If you didn't delete your old config and you want to try the upgrade again you should be able to:

Tony763 commented 9 months ago

Just tried. Integration starts, but virtual.yaml was not created and no components were moved.

twrecked commented 9 months ago

I'll give it a go here.

twrecked commented 9 months ago

It worked here for me. Assuming you currently have the latest virtual component installed, these are the steps I took:

And virtual.yaml was created.

I recommend turning on debug for the virtual component before doing this, that way you can poke around in the logs and make sure it's doing the work we expect it to.

logger:
  default: info
  logs:
    custom_components.virtual: debug

You should see lines like this appearing in the log:

2024-02-03 15:02:16.367 DEBUG (MainThread) [custom_components.virtual] importing a YAML setup
 ..
2024-02-03 15:02:16.396 INFO (MainThread) [custom_components.virtual.cfg] devices={..your devices here...}]}
2024-02-03 15:02:16.396 DEBUG (MainThread) [custom_components.virtual.cfg] uid=steve_presence
2024-02-03 15:02:16.396 DEBUG (MainThread) [custom_components.virtual.cfg] eid=binary_sensor.virtual_steve_presence
... more devices here

Hopefully that brings your config in.

Tony763 commented 9 months ago

Found it:

2024-02-04 00:28:36.515 DEBUG (MainThread) [custom_components.virtual.cfg] couldn't save user data [Errno 2] No such file or directory: '/config/virtual.yaml'
2024-02-04 00:28:36.516 DEBUG (MainThread) [custom_components.virtual.cfg] no meta data yet [Errno 2] No such file or directory: '/config/.storage/virtual.meta.json'

These two should be ERROR not DEBUG :slightly_smiling_face:

Configuration try to write to nonexist directory /config as my HA configuration is inside /home/homeassistant/.homeassistant/ directory.

btw, isn't aarlo typo in _LOGGER.debug(f"importing aarlo YAML {import_data}") ?

Tony763 commented 9 months ago

I see, they are hardcoded:

IMPORTED_YAML_FILE = "/config/virtual.yaml"
META_JSON_FILE = "/config/.storage/virtual.meta.json"
Tony763 commented 9 months ago

To get actual config path, You could use hass.config.path()) function in setup:

async def async_setup(......
    _LOGGER.error(hass.config.path())
    _LOGGER.error(hass.config.path(".storage"))

Returns:

image

88 could be related to this

twrecked commented 9 months ago

I was just going to say this was down to my naivety!

I'll update them later this evening.

twrecked commented 9 months ago

Sorry this is a bit of pain but can you try again?

I've tested this on my set up but I just have everything under /config.

Tony763 commented 9 months ago

No problem, last change works. virtual.yaml was created and everything was importet. Thank You

image