rgerganov / py-air-control

Command line app for controlling Philips air purifiers
MIT License
256 stars 52 forks source link

Error connecting to a Philips 1214/60 via HTTP and CoAP #97

Closed vihung closed 2 years ago

vihung commented 2 years ago

I have a Philips 1214/60 air purifier. I have set it up according to Philips's instructions - it is connected to my WiFi network and the Philips CleanHome+ app on my iPhone. I have not created a Philips login (yet) and it is working on my local network.

The app shows my device firmware version as 2.0.0, and wifi firmware version as 59.

My router shows me the IP Address of the purifier as 192.168.4.97

I am on a Mac (Mid-2011 Mac mini running macOS High Sierra 10.13.6).

I have installed python3 using Homebrew.

$ python3 --version
Python 3.9.13

I have installed py-air-control using PIP

sudo pip3 install py-air-control

Attempting to connect to the device using airctrl gives me an connection error.

Via HTTP;

$ airctrl --ipaddr 192.168.4.97 --protocol http
Exchanging secret key with the device ...
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1346, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1285, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1331, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1280, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1040, in _send_output
    self.send(msg)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 980, in send
    self.connect()
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 946, in connect
    self.sock = self._create_connection(
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
    raise err
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/airctrl", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/pyairctrl/airctrl.py", line 201, in main
    c = HTTPAirCli(device["ip"])
  File "/usr/local/lib/python3.9/site-packages/pyairctrl/airctrl.py", line 116, in __init__
    super().__init__(HTTPAirClient(host, debug))
  File "/usr/local/lib/python3.9/site-packages/pyairctrl/http_client.py", line 111, in __init__
    self.load_key()
  File "/usr/local/lib/python3.9/site-packages/pyairctrl/http_client.py", line 159, in load_key
    self._get_key()
  File "/usr/local/lib/python3.9/site-packages/pyairctrl/http_client.py", line 122, in _get_key
    with urllib.request.urlopen(req) as response:
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
    response = self._open(req, data)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1375, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1349, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 61] Connection refused>

and via CoAP;

$ airctrl --ipaddr 192.168.4.97 --protocol coap
Unexpected error:'Thread' object has no attribute 'isAlive'
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 980, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/coapthon/client/coap.py", line 273, in receive_datagram
    self._send_ack(transaction)
  File "/usr/local/lib/python3.9/site-packages/coapthon/client/coap.py", line 307, in _send_ack
    self.send_datagram(ack)
  File "/usr/local/lib/python3.9/site-packages/coapthon/client/coap.py", line 170, in send_datagram
    if self._receiver_thread is None or not self._receiver_thread.isAlive():
AttributeError: 'Thread' object has no attribute 'isAlive'

What can I do to make this work?

My overall intention is to use the homebridge-philips-air plugin for Homebridge, which in turn uses py-air-control.

rgerganov commented 2 years ago

The error you get when using CoAP is due to a bug in the coap library. I have fixed this in my Coapthon3 fork here: https://github.com/rgerganov/CoAPthon3/commit/21243904be7794dbea1857dc2d5724a344e35548

Install my fork like this:

pip3 install -U git+https://github.com/rgerganov/CoAPthon3

and then try again

vihung commented 2 years ago

I had missed a step in my above comment. I had previously installed a specific CoAPthon version - using sudo pip3 install -U git+https://github.com/Tanganelli/CoAPthon3@89d5173 (according to instructions at https://github.com/NikDevx/homebridge-philips-air). I will try your specific fork and report

vihung commented 2 years ago

I have installed the specific fork mentioned above

sudo pip3 install -U git+https://github.com/rgerganov/CoAPthon3@2124390

and then tried running airctrl. I still get an error

$ airctrl --ipaddr 192.168.4.97 --protocol coap
Unexpected error:'Thread' object has no attribute 'isAlive'
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 980, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/coapthon/client/coap.py", line 273, in receive_datagram
    self._send_ack(transaction)
  File "/usr/local/lib/python3.9/site-packages/coapthon/client/coap.py", line 307, in _send_ack
    self.send_datagram(ack)
  File "/usr/local/lib/python3.9/site-packages/coapthon/client/coap.py", line 170, in send_datagram
    if self._receiver_thread is None or not self._receiver_thread.isAlive():
AttributeError: 'Thread' object has no attribute 'isAlive'
vihung commented 2 years ago

Looking at your change (https://github.com/rgerganov/CoAPthon3/commit/21243904be7794dbea1857dc2d5724a344e35548), and looking at the error above ('Thread' object has no attribute 'isAlive'), it looks like some change has not stuck (is_alive vs. isAlive). I will investigate further.

Do I need to explicitly uninstall the previous other version of CoAPthon before installing yours?

(I am unfamiliar with most of this technology stack, although I am somewhat computer-savvy otherwise)

vihung commented 2 years ago

Yes. I can confirm the change had not stuck. I manually changed the isAlive() to is_alive().

sudo nano /usr/local/lib/python3.9/site-packages/coapthon/client/coap.py

and I now get this.

$ airctrl --ipaddr 192.168.4.97 --protocol coap
[name]                        Name: Main Bedroom Purifier
[type]                        Type: AC1214
[modelid]                     ModelId: AC1214/60
[swversion]                   Version: 2.0.0
[om]                          Fan speed: 1
[pwr]                         Power: OFF
[cl]                          Child lock: False
[aqil]                        Light brightness: 0
[uil]                         Buttons light: OFF
[mode]                        Mode: night
[pm25]                        PM25: 5
[iaql]                        Allergen index: 2
[aqit]                        Air quality notification threshold: 4
[ddp]                         Used index: IAI
[fltt1]                       HEPA filter type: NanoProtect Filter Series 3 (FY2422)
[fltt2]                       Active carbon filter type: NanoProtect Filter AC (FY2420)
[fltsts0]                     Pre-filter and Wick: clean in 346 hours
[fltsts1]                     HEPA filter: replace in 4786 hours
[fltsts2]                     Active carbon filter: replace in 2386 hours
[ota]                         Over the air updates: no
[Runtime]                     Runtime: 21.54 hours
[WifiVersion]                 WifiVersion: AWS_Philips_AIR@59
[ProductId]                   ProductId: e9dde280ff6d11e8bc6802024953075e
[DeviceId]                    DeviceId: 9543fe48df6711ec86899689d38c6000
[StatusType]                  StatusType: localcontrol
[ConnectType]                 ConnectType: Localcontrol

Thank you so much for your help!

Can you please let me know what I need to do to uninstall and reinstall the specific version of CoAPthon?

rgerganov commented 2 years ago

Good to hear it works. Managing python dependencies is a mess, sorry cannot help here.