Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Z-Wave JS - entities disappear if zwave-js server restarts #46180

Closed
steve28 opened this issue Feb 7, 2021 · 16 comments · Fixed by home-assistant-libs/zwave-js-server-python#109

Comments

@steve28
Copy link

steve28 commented Feb 7, 2021

The problem

When the zwave-js server restarts, some entities in Home Assistant disappear. In addition, the "reload" menu option for the configuration also disappears, thus requiring a restart of HA to get everything back.

What is version of Home Assistant Core has the issue?

2021.2.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Z-Wave JS

Link to integration documentation on our website

https://www.home-assistant.io/integrations/zwave_js

Example YAML snippet

N/A

Anything in the logs that might be useful for us?

N/A

Tagging @jcam per request

@jcam
Copy link
Contributor

jcam commented Feb 7, 2021

I also experience this, I am using HASS OS with a zwavejs2mqtt add-on.
Will try to gather logs tonight

@probot-home-assistant
Copy link

Hey there @home-assistant/z-wave, mind taking a look at this issue as its been labeled with an integration (zwave_js) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

@steve28
Copy link
Author

steve28 commented Feb 7, 2021

As additional information, I am running the zwavejs2mqtt docker container with mqtt/gateway disabled

@MartinHjelmare
Copy link
Member

When the server restarts the integration will be reloaded. Currently that means the entities will be removed and then re-added when the server reports the corresponding nodes and values. So this is expected behavior currently.

There's an improvement in progress that will turn the entities unavailable instead of removing them when the integration is reloaded.

@jcam
Copy link
Contributor

jcam commented Feb 8, 2021

They should come back though, when the server reports them ready. What we are experiencing is that they don't come back, the integration doesn't attempt to reload, and the reload option is missing.

@marcelveldt
Copy link
Member

hmmm, I wonder if there's a difference between official Z-Wave server add-on and zwavejs2mqtt.
Maybe the zwavejs2mqtt doesn't restart the server cleanly when the addon/docker container is restarted.

Even if that's the case, the heartbeat built-in to aiohttp should kick in after 55 seconds.
Did you wait a few minutes to see if it detects the lost connection ?

@jcam
Copy link
Contributor

jcam commented Feb 8, 2021

I've tried giving it an hour or so...

@jcam
Copy link
Contributor

jcam commented Feb 8, 2021

Just tried to grab logs. My laptop version (which is older) grabbed tons of logs but also worked just fine.
The one that runs on my nuc device:

2021-02-08 07:41:28 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Z-Wave JS for zwave_js             
Traceback (most recent call last):                                                                                              
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 239, in async_setup                                       
    result = await component.async_setup_entry(hass, self)  # type: ignore                                                      
  File "/usr/src/homeassistant/homeassistant/components/zwave_js/__init__.py", line 182, in async_setup_entry                   
    await client.connect()                                                                                                      
  File "/usr/local/lib/python3.8/site-packages/zwave_js_server/client.py", line 81, in connect                                  
    await self._client.receive_json()                                                                                           
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_ws.py", line 291, in receive_json                                 
    data = await self.receive_str(timeout=timeout)                                                                              
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_ws.py", line 276, in receive_str                                     
    raise TypeError(f"Received message {msg.type}:{msg.data!r} is not str")                                  
TypeError: Received message 257:None is not str   

@jcam
Copy link
Contributor

jcam commented Feb 8, 2021

I noticed in the zwavejs2mqtt control panel, the home ID and home hex were blank during the first few stages of the interview. could be related, or could be unrelated.

@jcam
Copy link
Contributor

jcam commented Feb 8, 2021

Updated my laptop version to 2021.2.1, got the same thing:

2021-02-08 07:52:02 DEBUG (MainThread) [zwave_js_server] Listen completed. Cleaning up
2021-02-08 07:52:02 INFO (MainThread) [homeassistant.components.zwave_js] Disconnected from server. Reloading integration
2021-02-08 07:52:02 DEBUG (MainThread) [zwave_js_server] Trying to connect
2021-02-08 07:52:02 WARNING (MainThread) [homeassistant.config_entries] Config entry 'Z-Wave JS' for zwave_js integration not ready yet. Retrying in 5 seconds
2021-02-08 07:52:07 DEBUG (MainThread) [zwave_js_server] Trying to connect
2021-02-08 07:52:08 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Z-Wave JS for zwave_js
Traceback (most recent call last):
  File "/prs/zwave/core/homeassistant/config_entries.py", line 239, in async_setup
    result = await component.async_setup_entry(hass, self)  # type: ignore
  File "/prs/zwave/core/homeassistant/components/zwave_js/__init__.py", line 182, in async_setup_entry
    await client.connect()
  File "/prs/zwave/core/venv/lib/python3.9/site-packages/zwave_js_server/client.py", line 81, in connect
    await self._client.receive_json()
  File "/prs/zwave/core/venv/lib/python3.9/site-packages/aiohttp/client_ws.py", line 291, in receive_json
    data = await self.receive_str(timeout=timeout)
  File "/prs/zwave/core/venv/lib/python3.9/site-packages/aiohttp/client_ws.py", line 276, in receive_str
    raise TypeError(f"Received message {msg.type}:{msg.data!r} is not str")
TypeError: Received message 257:None is not str

@marcelveldt
Copy link
Member

I noticed in the zwavejs2mqtt control panel, the home ID and home hex were blank during the first few stages of the interview. could be related, or could be unrelated.

That's what I expected. This is a race condition. The reconnect happens when the server is just not ready yet and thus we get unexpected results. We should guard the code that we only continue if all expecting objects are present and else consider it not yet ready.

@jcam
Copy link
Contributor

jcam commented Feb 8, 2021

Besides the home id, what else should be included in such a guard?

I see you're already checking for server version here https://github.com/home-assistant-libs/zwave-js-server-python/blob/master/zwave_js_server/client.py#L93

@MartinHjelmare
Copy link
Member

@marcelveldt Does the server need updating to solve the TypeError?

@marcelveldt
Copy link
Member

@marcelveldt Does the server need updating to solve the TypeError?

No, the server just dumps whatever is (already) there. What I suspect is that we just a get a partial dump if the above happens. So maybe we should check for a few properties to exist before assuming the connection is successful.
Looking at the above error, we seem to retrieve a malformed message (so not even an incomplete one).

From everything I read above, the python lib connects to the server just before it is even ready to dump something and will throw an error OR incomplete dump.

@MartinHjelmare
Copy link
Member

Line 81 in the stack trace is still inside the Python client connect method. We don't have any data yet from the server at that point.

@rccoleman
Copy link
Contributor

rccoleman commented Feb 9, 2021

Same/similar thing here with 2021.2.2 after restarting my custom zwjs2mqtt container:

2021-02-08 17:04:23 DEBUG (MainThread) [zwave_js_server] Listen completed. Cleaning up
2021-02-08 17:04:23 INFO (MainThread) [homeassistant.components.zwave_js] Disconnected from server. Reloading integration
2021-02-08 17:04:23 DEBUG (MainThread) [zwave_js_server] Trying to connect
2021-02-08 17:04:24 WARNING (MainThread) [homeassistant.config_entries] Config entry 'Z-Wave JS' for zwave_js integration not ready yet. Retrying in 5 seconds
2021-02-08 17:04:29 DEBUG (MainThread) [zwave_js_server] Trying to connect
2021-02-08 17:04:29 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Z-Wave JS for zwave_js
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 239, in async_setup
    result = await component.async_setup_entry(hass, self)  # type: ignore
  File "/usr/src/homeassistant/homeassistant/components/zwave_js/__init__.py", line 182, in async_setup_entry
    await client.connect()
  File "/usr/local/lib/python3.8/site-packages/zwave_js_server/client.py", line 81, in connect
    await self._client.receive_json()
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_ws.py", line 291, in receive_json
    data = await self.receive_str(timeout=timeout)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_ws.py", line 276, in receive_str
    raise TypeError(f"Received message {msg.type}:{msg.data!r} is not str")
TypeError: Received message 257:None is not str
2021-02-08 17:11:11 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [139856284059392] Error handling message: Unknown error
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/connection.py", line 95, in async_handle
    handler(self.hass, self, schema(msg))
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 50, in with_admin
    func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/zwave_js/api.py", line 50, in websocket_network_status
    client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
KeyError: 'bc9765f242063d943b466eccfa2fbcdd'

Had to restart HA to get the network up and running, after which is was fine.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants