fhempy / fhempy

fhempy is a FHEM binding to write modules in Python language
MIT License
45 stars 20 forks source link

homekit.py:147: RuntimeWarning: coroutine 'CommandAttr' was never awaited #292

Closed petr-tichy closed 6 months ago

petr-tichy commented 6 months ago

https://github.com/fhempy/fhempy/blob/b2a7729cd23ea325b5b515c66012f91c99350556/FHEM/bindings/python/fhempy/lib/homekit/homekit.py#L147C18-L147C18

When fixed with await I get into another issue:


2024-01-07 18:40:01,682 - ERROR    - fhempy.lib.fhem_pythonbinding: Failed to execute function Attr: Traceback (most recent call last):
  File "/opt/fhem/.fhempy/fhempy_venv/lib/python3.11/site-packages/fhempy/lib/fhem_pythonbinding.py", line 385, in handle_function
    ret = await self.execute_function(hash, fhem_reply_done, nmInstance)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/fhem/.fhempy/fhempy_venv/lib/python3.11/site-packages/fhempy/lib/fhem_pythonbinding.py", line 484, in execute_function
    ret = await asyncio.wait_for(
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/tasks.py", line 479, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/opt/fhem/.fhempy/fhempy_venv/lib/python3.11/site-packages/fhempy/lib/generic.py", line 204, in Attr
    return await utils.handle_attr(self._conf_attr, self, hash, args, argsh)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/fhem/.fhempy/fhempy_venv/lib/python3.11/site-packages/fhempy/lib/utils.py", line 108, in handle_attr
    ret = await fct_call(hash)
          ^^^^^^^^^^^^^^^^^^^^
  File "/opt/fhem/.fhempy/fhempy_venv/lib/python3.11/site-packages/fhempy/lib/homekit/homekit.py", line 135, in set_attr_pairing_data
    await self.setup_gateway_device(hash["HOMEKIT_ID"], hash["HOMEKIT_PIN"])
                                    ~~~~^^^^^^^^^^^^^^
KeyError: 'HOMEKIT_ID'
 with hash: {'defargsh': {}, 'function': 'Attr', 'args': ['set', 'dirigera', 'pairing_data', "{'AccessoryPairingID': 'AD:D9:70:...', 'AccessoryLTPK': '...', 'iOSPairingId': '...', 'iOSDeviceLTSK': '...', 'iOSDeviceLTPK': '...', 'AccessoryIP': '...', 'AccessoryPort'
: 8000, 'Connection': 'IP'}"], 'FHEMPYTYPE': 'homekit', 'msgtype': 'function', 'argsh': {}, 'NAME': 'dirigera', 'defargs': ['dirigera', 'fhempy', 'homekit', 'AD:D9:7
0:...', '123-45-678'], 'id': 2916776, 'PYTHONTYPE': 'homekit'}

I'm trying to pair IKEA DIRIGERA.

fhempy commented 6 months ago

Hi, thanks for reporting! I fixed the await.

Can you please provide your define statement? It looks like the HOMEKIT_ID and PIN is missing in the define.

fhempy commented 6 months ago

Please also try to delete the created devices in FHEM and do the define again with the fixed version.

petr-tichy commented 6 months ago

def dirigera fhempy homekit AD:D9:70:... 123-45-678 (not the actual values)

petr-tichy commented 6 months ago

HOMEKIT_ID and HOMEKIT_PIN are there in FHEM internals

fhempy commented 6 months ago

Please update to the latest fhempy version, delete all homekit devices in FHEM and try again. It could be that the missing await caused the error.

petr-tichy commented 6 months ago

Now at 0.1.697 and KeyError: 'HOMEKIT_ID' is still there at pairing attempt. About to start VSCode debug.

fhempy commented 6 months ago

That's really strange. 2 lines above hash["HOMEKIT_ID"] is assigned. The only issue could be that args[3] is None.

petr-tichy commented 6 months ago

I'm not sure this is the root cause, but json.loads(value) throws JSONDecodeError('Expecting property name enclosed in double quotes: line 1 column 2 (char 1)') as the value is "{'AccessoryPairingID': 'AD:D9:70.... when retrieving pairing_data This prevents restoring pairing_data and not paired yet path is always chosen at https://github.com/fhempy/fhempy/blob/42968b234e7372e8a7059feb112b690909c17e33/FHEM/bindings/python/fhempy/lib/homekit/homekit.py#L142C13-L142C29

petr-tichy commented 6 months ago

The #297 fixes the above pairing issue, and #298 fixes naming of devices. With these two, I was able to pair Dirigera and the devices appeared in FHEM. All seems good now.

fhempy commented 6 months ago

Great to see that it is working for you! Thank you very much for your contributions!

Just to let you know, the homekit implementation is currently very very basic. The most important thing which is missing is the update loop to poll the current states.