jlusiardi / homekit_python

A python implementation to work as both HomeKit controller and accessory.
Apache License 2.0
221 stars 41 forks source link

Support Windows? #185

Open a3135134 opened 4 years ago

a3135134 commented 4 years ago

I use win 10 and install this python module. However, it raises ModuleNotFoundError: No module named '_crypt' . It seems _crypt only work on Unix/Linux, so this library also only support Linux?

jlusiardi commented 4 years ago

Hi, This module should also work under windows, but only for IP based accessories, no Bluetooth LE support. I have a few questions:

a3135134 commented 4 years ago

I used cmd " pip install homekit". When I try python -m homekit.discover, here's the log:

Traceback (most recent call last): File "E:\Python3\lib\runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "E:\Python3\lib\runpy.py", line 109, in _get_module_details import(pkg_name) File "E:\Python3\lib\site-packages\homekit__init.py", line 26, in from homekit.controller import Controller File "E:\Python3\lib\site-packages\homekit\controller\init.py", line 21, in from homekit.controller.controller import Controller File "E:\Python3\lib\site-packages\homekit\controller\controller.py", line 26, in from homekit.protocol.tlv import TLV File "E:\Python3\lib\site-packages\homekit\protocol\init.py", line 31, in from homekit.crypto import chacha20_aead_decrypt, chacha20_aead_encrypt, SrpClient File "E:\Python3\lib\site-packages\homekit\crypto\init__.py", line 22, in from homekit.crypto.srp import SrpClient, SrpServer File "E:\Python3\lib\site-packages\homekit\crypto\srp.py", line 23, in import crypt File "E:\Python3\lib\crypt.py", line 3, in import _crypt ModuleNotFoundError: No module named '_crypt'

jlusiardi commented 4 years ago

Mh interesting point, looks like the crypt module is not available on windows (https://docs.python.org/3/library/crypt.html). Because only crypt.mksalt is needed, we might reimplement that?

a3135134 commented 4 years ago

Yes. Might choose a cross-platform python lib.

Jc2k commented 4 years ago

It looks like crypt.mksalt is justed used to generate random 16-byte numbers needed by Srp as salt and keys. One of the crypto libraries we already depend on (cryptography) has some advice in that case:

https://cryptography.io/en/latest/random-numbers/

It should be fine to replace mksalt with:

int.from_bytes(os.urandom(16), byteorder="big")

On python 3.6 and later an option would be:

int.from_bytes(secrets.token_bytes(16), byteorder="big")

No new libraries required.

jlusiardi commented 4 years ago

@Jc2k sounds good, this should be the solution.

jlusiardi commented 4 years ago

Hey @a3135134,

can you try with branch fix_185_homekit_on_windows? I removed the dependency to crypt in that.

a3135134 commented 4 years ago

Hey @a3135134,

can you try with branch fix_185_homekit_on_windows? I removed the dependency to crypt in that.

I placed the homekit directory into python lib.

python -m homekit.discover Traceback (most recent call last): File "E:\Python3\lib\runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "E:\Python3\lib\runpy.py", line 109, in _get_module_details import(pkg_name) File "E:\Python3\lib\site-packages\homekit__init.py", line 26, in from homekit.controller import Controller File "E:\Python3\lib\site-packages\homekit\controller\init__.py", line 21, in from homekit.controller.controller import Controller File "E:\Python3\lib\site-packages\homekit\controller\controller.py", line 23, in import tlv8 ModuleNotFoundError: No module named 'tlv8'

I tried pip install tlv8. Here's a different error File "E:\Python3\lib\site-packages\zeroconf.py", line 1817, in init self._listen_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, _value) OSError: [WinError 10042]

jlusiardi commented 4 years ago

Fixed the issue with tlv8.

Is there nothing behind the OSError: [WinError 10042]?

jlusiardi commented 4 years ago

close was not intended?

a3135134 commented 4 years ago

Fixed the issue with tlv8.

Is there nothing behind the OSError: [WinError 10042]?

I think this is another problem. I'll have a check first OSError: [WinError 10042] An unknown, invalid, or unsupported option or level specified in the getsockopt or setsockopt call.

jlusiardi commented 4 years ago

So I was able to install homekit_python on a win 10 VM.

  1. Python 3.8.2 and git via https://chocolatey.org/
  2. create virtual env: py.exe -m venv venv
  3. Allow script execution: Set-ExecutionPolicy Bypass -Scope Process -Force;
  4. activate virtual env: .\venv\Scripts\activate
  5. Clone Repo: &'C:\Program Files\Git\bin\git.exe' clone https://github.com/jlusiardi/homekit_python.git
  6. cd homekit_python
  7. Checkout branch: &'C:\Program Files\Git\bin\git.exe' checkout fix_185_homekit_on_windows
  8. Install C++-Buildtools from Microsoft (https://visualstudio.microsoft.com/de/visual-cpp-build-tools/, required for install ed25519 python package)
  9. Install the branch: pip3 install .[IP]
  10. Use py.exe -m homekit.discover to check for homekit accessories. This wants to access network, so you might want to allow that.

Results: One of my accessories is found

Name: Koogeek-P1-770D90._hap._tcp.local.
Url: http_impl://192.168.178.200:80
Configuration number (c#): 2
Feature Flags (ff): Supports HAP Pairing (Flag: 1)
Device ID (id): 44:AF:68:9E:5D:17
Model Name (md): P1EU
Protocol Version (pv): 1.1
State Number (s#): 1
Status Flags (sf): Accessory has been paired. (Flag: 0)
Category Identifier (ci): Outlet (Id: 7)
jlusiardi commented 4 years ago

@a3135134 does this work for you?