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

Switch from RPi.GPIO to gpiozero #125

Merged
merged 7 commits into from
Aug 5, 2022
Merged

Conversation

puddly
Copy link
Contributor

@puddly puddly commented Aug 1, 2022

home-assistant/core#75967

gpiozero is pure-Python and can be installed on any platform, removing the need for the conditional requirements in the setup.py file.

I don't have a GPIO or DIN ZiGate to test with so this is a blind implementation. It should work but I would appreciate someone testing it out.

@puddly
Copy link
Contributor Author

puddly commented Aug 2, 2022

I've confirmed the pin states are correct with an oscilloscope hooked up to a Pi

Below is a sequence of entering flashing mode, waiting three seconds, then booting normally:

image

Interestingly, the original code's timing using RPi.GPIO directly does not seem to actually match what was written:

image

@puddly
Copy link
Contributor Author

puddly commented Aug 10, 2022

@pipiche38 can you test out zigpy-zigate==0.9.1 with a PiZiGate to confirm this change works? You can use zigpy-cli if you don't want to spin up an entire HA installation:

$ pip install git+https://github.com/zigpy/zigpy-cli.git
$ pip install zigpy-zigate==0.9.1  # new version
$ zigpy -vv radio zigate /dev/ttyAMA0 info

@zigbeefordomoticz
Copy link

zigbeefordomoticz commented Aug 10, 2022 via email

@pipiche38
Copy link

Test done on a Fedora distribution

The Pi ZiGate was already in running mode , so I guess the GPIO config was already good

[support@rasp ~]$ zigpy -vv radio zigate /dev/ttyUSBRPI3 info
2022-08-16 19:19:30.026 rasp asyncio DEBUG Using selector: EpollSelector
2022-08-16 19:19:31.593 rasp zigpy.appdb DEBUG SQLite version for <module 'sqlite3' from '/usr/lib/python3.9/sqlite3/__init__.py'>: 3.34.1
2022-08-16 19:19:31.907 rasp zigpy_zigate.uart DEBUG PiZiGate detected
2022-08-16 19:19:31.911 rasp zigpy_zigate.common INFO Put PiZiGate in running mode
2022-08-16 19:19:31.913 rasp zigpy_zigate.uart DEBUG Connection made
2022-08-16 19:19:31.914 rasp asyncio ERROR Future exception was never retrieved
future: <Future finished exception=TypeError("__init__() got an unexpected keyword argument 'initial_state'")>
Traceback (most recent call last):
  File "/usr/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_zigate/common.py", line 84, in set_pizigate_running_mode
    gpio0 = UnclosableOutputDevice(pin=GPIO_PIN0, initial_state=None)
  File "/home/support/.local/lib/python3.9/site-packages/gpiozero/devices.py", line 108, in __call__
    self = super(GPIOMeta, cls).__call__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'initial_state'
2022-08-16 19:19:31.917 rasp zigpy_zigate.api DEBUG Sending CommandId.SET_RAWMODE (b'\x01'), waiting for status: True, waiting for response: None
2022-08-16 19:19:31.918 rasp zigpy_zigate.uart DEBUG Send: 0x0002 b'01'
2022-08-16 19:19:31.919 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\x02\x00\x01\x02\x01'
2022-08-16 19:19:31.920 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x02\x12\x02\x10\x02\x11\x02\x12\x02\x11'
2022-08-16 19:19:31.921 rasp zigpy_zigate.api DEBUG Wait for status to command CommandId.SET_RAWMODE
2022-08-16 19:19:31.932 rasp zigpy_zigate.uart DEBUG Frame received: 800000098b000000020000000000
2022-08-16 19:19:31.932 rasp zigpy_zigate.api DEBUG data received 0x8000 b'0000000200000000' LQI:0
2022-08-16 19:19:31.933 rasp zigpy_zigate.zigbee.application DEBUG zigate_callback_handler ResponseId.STATUS [<Status.Success: 0>, 0, 2, b'\x00\x00\x00\x00']
2022-08-16 19:19:31.935 rasp zigpy_zigate.api DEBUG Sending CommandId.SET_TIMESERVER (b'*\x8e\xaaC'), waiting for status: True, waiting for response: None
2022-08-16 19:19:31.935 rasp zigpy_zigate.uart DEBUG Send: 0x0016 b'2a8eaa43'
2022-08-16 19:19:31.936 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\x16\x00\x04_*\x8e\xaaC'
2022-08-16 19:19:31.936 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x16\x02\x10\x02\x14_*\x8e\xaaC'
2022-08-16 19:19:31.937 rasp zigpy_zigate.api DEBUG Wait for status to command CommandId.SET_TIMESERVER
2022-08-16 19:19:31.947 rasp zigpy_zigate.uart DEBUG Frame received: 800000099f000000160000000000
2022-08-16 19:19:31.947 rasp zigpy_zigate.api DEBUG data received 0x8000 b'0000001600000000' LQI:0
2022-08-16 19:19:31.948 rasp zigpy_zigate.zigbee.application DEBUG zigate_callback_handler ResponseId.STATUS [<Status.Success: 0>, 0, 22, b'\x00\x00\x00\x00']
2022-08-16 19:19:31.949 rasp zigpy_zigate.api DEBUG Sending CommandId.GET_VERSION (b''), waiting for status: True, waiting for response: ResponseId.VERSION_LIST
2022-08-16 19:19:31.950 rasp zigpy_zigate.uart DEBUG Send: 0x0010 b''
2022-08-16 19:19:31.950 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\x10\x00\x00\x10'
2022-08-16 19:19:31.951 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x10\x02\x10\x02\x10\x10'
2022-08-16 19:19:31.952 rasp zigpy_zigate.api DEBUG Wait for status to command CommandId.GET_VERSION
2022-08-16 19:19:31.953 rasp zigpy_zigate.api DEBUG Wait for response ResponseId.VERSION_LIST
2022-08-16 19:19:31.961 rasp zigpy_zigate.uart DEBUG Frame received: 8000000999000000100000000000
2022-08-16 19:19:31.961 rasp zigpy_zigate.api DEBUG data received 0x8000 b'0000001000000000' LQI:0
2022-08-16 19:19:31.962 rasp zigpy_zigate.zigbee.application DEBUG zigate_callback_handler ResponseId.STATUS [<Status.Success: 0>, 0, 16, b'\x00\x00\x00\x00']
2022-08-16 19:19:31.963 rasp zigpy_zigate.uart DEBUG Frame received: 80100005b50003032000
2022-08-16 19:19:31.963 rasp zigpy_zigate.api DEBUG data received 0x8010 b'00030320' LQI:0
2022-08-16 19:19:31.964 rasp zigpy_zigate.zigbee.application DEBUG zigate_callback_handler ResponseId.VERSION_LIST [3, 800]
2022-08-16 19:19:31.965 rasp zigpy_zigate.zigbee.application WARNING Old ZiGate firmware detected, you should upgrade to 3.21 or newer
2022-08-16 19:19:31.966 rasp zigpy_zigate.api DEBUG Sending CommandId.NETWORK_STATE_REQ (b''), waiting for status: True, waiting for response: ResponseId.NETWORK_STATE_RSP
2022-08-16 19:19:31.967 rasp zigpy_zigate.uart DEBUG Send: 0x0009 b''
2022-08-16 19:19:31.967 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\t\x00\x00\t'
2022-08-16 19:19:31.967 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x02\x19\x02\x10\x02\x10\x02\x19'
2022-08-16 19:19:31.969 rasp zigpy_zigate.api DEBUG Wait for status to command CommandId.NETWORK_STATE_REQ
2022-08-16 19:19:31.969 rasp zigpy_zigate.api DEBUG Wait for response ResponseId.NETWORK_STATE_RSP
2022-08-16 19:19:33.471 rasp zigpy_zigate.uart DEBUG Send: 0x0009 b''
2022-08-16 19:19:33.472 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\t\x00\x00\t'
2022-08-16 19:19:33.473 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x02\x19\x02\x10\x02\x10\x02\x19'
2022-08-16 19:19:34.978 rasp zigpy_zigate.uart DEBUG Send: 0x0009 b''
2022-08-16 19:19:34.979 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\t\x00\x00\t'
2022-08-16 19:19:34.980 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x02\x19\x02\x10\x02\x10\x02\x19'
2022-08-16 19:19:34.991 rasp zigpy_zigate.uart DEBUG Frame received: 8000000980000000090000000000
2022-08-16 19:19:34.992 rasp zigpy_zigate.api DEBUG data received 0x8000 b'0000000900000000' LQI:0
2022-08-16 19:19:34.993 rasp zigpy_zigate.zigbee.application DEBUG zigate_callback_handler ResponseId.STATUS [<Status.Success: 0>, 0, 9, b'\x00\x00\x00\x00']
2022-08-16 19:19:34.995 rasp zigpy_zigate.uart DEBUG Frame received: 8009001688000000158d0001c61bbbf0812d7341c48165902c1a00
2022-08-16 19:19:34.996 rasp zigpy_zigate.api DEBUG data received 0x8009 b'000000158d0001c61bbbf0812d7341c48165902c1a' LQI:0
2022-08-16 19:19:34.997 rasp zigpy_zigate.zigbee.application DEBUG zigate_callback_handler ResponseId.NETWORK_STATE_RSP [0x0000, 00:15:8d:00:01:c6:1b:bb, 61569, 3275033666268794924, 26]
PAN ID:                0xF081
Extended PAN ID:       2d:73:41:c4:81:65:90:2c
Channel:               26
Channel mask:          [26]
NWK update ID:         0
Device IEEE:           00:15:8d:00:01:c6:1b:bb
Device NWK:            0x0000
Network key:           ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff
Network key sequence:  0
Network key counter:   0
2022-08-16 19:19:35.004 rasp zigpy_zigate.api DEBUG Sending CommandId.RESET (b''), waiting for status: True, waiting for response: None
2022-08-16 19:19:35.005 rasp zigpy_zigate.uart DEBUG Send: 0x0011 b''
2022-08-16 19:19:35.006 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\x11\x00\x00\x11'
2022-08-16 19:19:35.007 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x11\x02\x10\x02\x10\x11'
2022-08-16 19:19:35.009 rasp zigpy_zigate.api DEBUG Wait for status to command CommandId.RESET
2022-08-16 19:19:35.018 rasp zigpy_zigate.uart DEBUG Frame received: 8000000998000000110000000000
2022-08-16 19:19:35.019 rasp zigpy_zigate.api DEBUG data received 0x8000 b'0000001100000000' LQI:0
2022-08-16 19:19:35.020 rasp zigpy_zigate.zigbee.application DEBUG zigate_callback_handler ResponseId.STATUS [<Status.Success: 0>, 0, 17, b'\x00\x00\x00\x00']
2022-08-16 19:19:35.023 rasp zigpy_zigate.uart DEBUG Closed serial connection

I force the GPIO to switch the Pi ZiGate into Flash mode, and I expect the zigpy-zigate layer to reset the pi-zigate to run mode

[support@rasp ~]$ zigpy -vv radio zigate /dev/ttyUSBRPI3 info
2022-08-16 19:20:54.259 rasp asyncio DEBUG Using selector: EpollSelector
2022-08-16 19:20:55.836 rasp zigpy.appdb DEBUG SQLite version for <module 'sqlite3' from '/usr/lib/python3.9/sqlite3/__init__.py'>: 3.34.1
2022-08-16 19:20:56.161 rasp zigpy_zigate.uart DEBUG PiZiGate detected
2022-08-16 19:20:56.167 rasp zigpy_zigate.common INFO Put PiZiGate in running mode
2022-08-16 19:20:56.172 rasp zigpy_zigate.uart DEBUG Connection made
2022-08-16 19:20:56.174 rasp asyncio ERROR Future exception was never retrieved
future: <Future finished exception=TypeError("__init__() got an unexpected keyword argument 'initial_state'")>
Traceback (most recent call last):
  File "/usr/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_zigate/common.py", line 84, in set_pizigate_running_mode
    gpio0 = UnclosableOutputDevice(pin=GPIO_PIN0, initial_state=None)
  File "/home/support/.local/lib/python3.9/site-packages/gpiozero/devices.py", line 108, in __call__
    self = super(GPIOMeta, cls).__call__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'initial_state'
2022-08-16 19:20:56.181 rasp zigpy_zigate.api DEBUG Sending CommandId.SET_RAWMODE (b'\x01'), waiting for status: True, waiting for response: None
2022-08-16 19:20:56.182 rasp zigpy_zigate.uart DEBUG Send: 0x0002 b'01'
2022-08-16 19:20:56.183 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\x02\x00\x01\x02\x01'
2022-08-16 19:20:56.185 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x02\x12\x02\x10\x02\x11\x02\x12\x02\x11'
2022-08-16 19:20:56.187 rasp zigpy_zigate.api DEBUG Wait for status to command CommandId.SET_RAWMODE
2022-08-16 19:20:57.690 rasp zigpy_zigate.uart DEBUG Send: 0x0002 b'01'
2022-08-16 19:20:57.691 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\x02\x00\x01\x02\x01'
2022-08-16 19:20:57.691 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x02\x12\x02\x10\x02\x11\x02\x12\x02\x11'
2022-08-16 19:20:59.195 rasp zigpy_zigate.uart DEBUG Send: 0x0002 b'01'
2022-08-16 19:20:59.197 rasp zigpy_zigate.uart DEBUG Frame to send: b'\x00\x02\x00\x01\x02\x01'
2022-08-16 19:20:59.198 rasp zigpy_zigate.uart DEBUG Frame escaped: b'\x02\x10\x02\x12\x02\x10\x02\x11\x02\x12\x02\x11'
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context().obj, *args, **kwargs)
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_cli/cli.py", line 19, in inner
    return loop.run_until_complete(cmd(*args, **kwargs))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_cli/radio.py", line 75, in info
    await app.connect()
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_zigate/zigbee/application.py", line 39, in connect
    await api.set_raw_mode()
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_zigate/api.py", line 409, in set_raw_mode
    await self.command(CommandId.SET_RAWMODE, data)
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_zigate/api.py", line 373, in command
    raise NoStatusError()
zigpy_zigate.api.NoStatusError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/support/.local/bin/zigpy", line 8, in <module>
    sys.exit(cli())
  File "/usr/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 404, in __exit__
    self.close()
  File "/usr/lib/python3.9/site-packages/click/core.py", line 494, in close
    cb()
  File "/usr/lib/python3.9/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context().obj, *args, **kwargs)
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_cli/cli.py", line 19, in inner
    return loop.run_until_complete(cmd(*args, **kwargs))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_cli/radio.py", line 66, in radio_cleanup
    await app.shutdown()
  File "/home/support/.local/lib/python3.9/site-packages/zigpy/application.py", line 206, in shutdown
    await self.disconnect()
  File "/home/support/.local/lib/python3.9/site-packages/zigpy_zigate/zigbee/application.py", line 52, in disconnect
    await self._api.reset(wait=False)
AttributeError: 'NoneType' object has no attribute 'reset'

@pipiche38
Copy link

And unfortunatly I'm not able to make the pizigate running on a RPI4 with debian

@puddly
Copy link
Contributor Author

puddly commented Aug 17, 2022

Can you try with the most recent PR (unmerged) fixing this typo?

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

Successfully merging this pull request may close these issues.

None yet

3 participants