leeyuentuen / localtuya

local handling for Tuya devices
GNU General Public License v3.0
72 stars 17 forks source link

Error in config flow with zigbee gateway #28

Closed gian72 closed 1 year ago

gian72 commented 1 year ago

After having sucessfully created zigbee gateway entity (through config flow), trying to call again configuration flow in order to add zigbee entities, I got an error message: ".. 500 Internal Server Error Server got itself in trouble"

Stack trace in the log is this (full log attached)

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
    resp = await request_handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
[Error.txt](https://github.com/leeyuentuen/localtuya/files/10347446/Error.txt)

  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 81, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 136, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 218, in post
    return await super().post(request)
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 73, in wrapper
    result = await method(view, request, data, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 71, in post
    result = await self._flow_mgr.async_init(
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 226, in async_init
    flow, result = await task
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 253, in _async_init
    result = await self._async_handle_step(flow, flow.init_step, data, init_done)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 368, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/config/custom_components/localtuya/config_flow.py", line 577, in async_step_init
    options_schema(self.entities), **self.config_entry.data
AttributeError: 'LocalTuyaOptionsFlowHandler' object has no attribute 'entities'
leeyuentuen commented 1 year ago

on which version was this?

gian72 commented 1 year ago

Hi Leeyuentuen, it happen on latest version on your master branch, but I think that similar issue happens also for older versions

leeyuentuen commented 1 year ago

ok, I'll take a look at it. In the meantime, you can use YAML to use Zigbee gateway?

gian72 commented 1 year ago

I could use YAML, but I don't know how to configure my thermostatic valves (I didn't find any example implementing climate platform)

nigam-sa commented 1 year ago

I am having the same issue. Tried adding the Gateway via YAML and it does adds there but doesn't show any device entry for that. Also adding subdevice from YAML doesn't work. Enabled debug and see below error entries -

2023-01-12 10:47:48.430 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf7...j3l] Decrypted payload: devid not found 2023-01-12 10:47:48.430 ERROR (MainThread) [custom_components.localtuya.pytuya] [bf7...j3l] Failed to get status for cid bfbfac5d0fba5f2a599mpn: Expecting value: line 1 column 1 (char 0) Traceback (most recent call last): File "/config/custom_components/localtuya/pytuya/init.py", line 684, in detect_available_dps status = await self.status(cid) File "/config/custom_components/localtuya/pytuya/init.py", line 598, in status status = await self.exchange(ACTION_STATUS, cid=cid) File "/config/custom_components/localtuya/pytuya/init.py", line 576, in exchange payload = self._decode_payload(msg.payload) File "/config/custom_components/localtuya/pytuya/init.py", line 788, in _decode_payload return json.loads(payload) File "/usr/local/lib/python3.10/json/init.py", line 346, in loads return _default_decoder.decode(s) File "/usr/local/lib/python3.10/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/lib/python3.10/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Anycubic commented 1 year ago

@gian72 and @nigam-sa which value are you providing when adding sub devices? I have TRV valves and no problems at all. I provide NODE-ID values, not local key values

nigam-sa commented 1 year ago

@Anycubic Thanks for pointing this out. Using Node-ID i was able to add the zigbee device and they are working as well so clearly zigbee gateway is working as well but as soon as I click configure under Zigbee Gateay i get the error - 'Config flow could not be loaded: 500 Internal Server Error Server got itself in trouble'

After spending sometime on debugging, this could be due to there isn't any configuration available for zigbee hub.

@leeyuentuen - Is it possible for you handle the below exception in File "/config/custom_components/localtuya/config_flow.py", line 577, in async_step_init

AttributeError: 'LocalTuyaOptionsFlowHandler' object has no attribute 'entities'

leeyuentuen commented 1 year ago

@Anycubic Thanks for pointing this out. Using Node-ID i was able to add the zigbee device and they are working as well so clearly zigbee gateway is working as well but as soon as I click configure under Zigbee Gateay i get the error - 'Config flow could not be loaded: 500 Internal Server Error Server got itself in trouble'

After spending sometime on debugging, this could be due to there isn't any configuration available for zigbee hub.

@leeyuentuen - Is it possible for you handle the below exception in File "/config/custom_components/localtuya/config_flow.py", line 577, in async_step_init

AttributeError: 'LocalTuyaOptionsFlowHandler' object has no attribute 'entities'

the entities issue is know issue. i need to fix them when i'm back from holiday. atm no laptop to troubleshooting

nigam-sa commented 1 year ago

Thanks for quick response @leeyuentuen . Enjoy your holidays. I also managed to add zigbee 2 channel dimmer after bit of hit and try.. will send you the details to update for others

gian72 commented 1 year ago

@Anycubic: my problem is that after adding gateway, I cannot add any other subdevices

nigam-sa commented 1 year ago

@gian72 i have tried adding gateway via YAML and UI method and both works for me and allows to add subdevice. Have you tried adding via UI method?

alexualbu commented 1 year ago

@gian72 , I have seen this issue when trying to reconfigure the gateway (same as @nigam-sa). Was this the same for you? The process to add sub-devices is the the normal flow: add integration + select (sub-device) from the 'discovered devices' list + parent gateway is populated (or I guess you select it if you have more than 1) @nigam-sa , looking at your exception you may be using the device id and not the cid (when you get the device information for a sub-device you should have both device id and cid keys)

gian72 commented 1 year ago

Hi @alexualbu, using config flow I can only add gateway, then, further operations (e.g. adding sub devices, have an early error). Trying with YAML, I added Gatetway, but didn't manage to add sub-devices

alexualbu commented 1 year ago

@gian72 , how are you trying to add the sub-devices and what is the error? I am doing it all via the config flow now because I couldn't get the YAML config right for the sub-devices. The steps should be, in the config flow:

nigam-sa commented 1 year ago

Hi @alexualbu, I was able to add both Gateway and subdevices. I used nodeid instead of device id to make subdevice work but everything is working except when you click on configure button for gateway after its added you get error but that's a known error and leeyuentuen mentioned it will be fixed once he's back.

Can you confirm how you get CID of subdevice as I can't get this from iot.tuya?

alexualbu commented 1 year ago

@nigam-sa, i get them either from iot.tuya (where they're called node_id) or tuya-cli. So it's the same thing.

For the error you can checkout my PR which fixes it - it's not extensively tested, but it works for me.

leeyuentuen commented 1 year ago

beta release: https://github.com/leeyuentuen/localtuya/releases/tag/3.6.4-beta.1

gian72 commented 1 year ago

Thank you @leeyuentuen, I did further steps:

leeyuentuen commented 1 year ago

should entities not in this example? a4c1385ae1d684ea

it should be node-id instead of cid for the sub-devices if i remember correctly

gian72 commented 1 year ago

a4c1385ae1d684ea is the id (also called node_id or uuid.. in my tuya.iot) of one TRV valve, that exposes these dps:

gian72 commented 1 year ago

With the above YAML, I can see correct values: Capture3 but obviously, if I dont' set "climate platform" I cannot set target temperature a neither see the right widget in dashboard

leeyuentuen commented 1 year ago

i mean that you put the node id in the config flow as entities instead of (keyfa7hya4gfa7g9)

gian72 commented 1 year ago

Yes I added a4c1385ae1d684ea, I didnt't see any error, but it seems to do anything (the only configured device is gateway)

alexualbu commented 1 year ago

@gian72 , you should not go into reconfigure for the gateway to add the TRV (what your seing there is actually the checkbox for is_gateway and the key is the product_key - @leeyuentuen , btw these labels are missing too). you need to add a new integration, select (sub_device) then add the node_id / cid there and it will go in the config flow for the entities.

image
gian72 commented 1 year ago

@alexualbu thanks! it works! :-) Now i just need to detect corrects status: "Heating" / "Pause"

alexualbu commented 1 year ago

great, @gian72 !

if you are talking about setting the mode then that is here in the climate.py file. If you want it to show the current state, the predefined values are below in HVAC_ACTIONS. My TRVs don't have a DP for the current state, but the current climate logic only works like that I believe. In my case I would need to change that code to update the status based on the valve opening % I guess.

gian72 commented 1 year ago

Thanks again @alexualbu @leeyuentuen now it works 100% with config flow :-)

BTW my TRV valves are these: https://m.it.aliexpress.com/item/1005003952150617.html?gatewayAdapt=glo2ita (quite good, even if it seems don't support partial opening)

leeyuentuen commented 1 year ago

great to hear, i'll close this issue and put the beta to release