arraylabs / pymyq

Python wrapper for MyQ API
MIT License
112 stars 42 forks source link

[Errno 104] Connection reset by peer #58

Closed jbronikowski closed 3 years ago

jbronikowski commented 3 years ago

Need a bit of troubleshooting help. I have tried to recreate this and cannot on any other container or machine. Only way to recreate it is to use the homeassistant/qemux86-64-homeassistant:0.118.4 docker container or the latest HA stable container.

pymyq stopped working within home assistant. I can curl and wget from the container to pymyq but cannot get it to make a connection outbound via the module.

I thought it had something to do with the openssl on the container but i upgraded it to the december 8th release

Test file i have been using.

import asyncio

from aiohttp import ClientSession

import pymyq
import logging
import sys

root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)

async def main() -> None:
    """Create the aiohttp session and run."""
    async with ClientSession() as websessions:
      myq = await pymyq.login('@gmail.com', 'hahlsajlsf', websessions)

      # Return only cover devices:
      devices = myq.covers
      # >>> {"serial_number123": <Device>}

      # Return *all* devices:
      devices = myq.devices
      # >>> {"serial_number123": <Device>, "serial_number456": <Device>}

asyncio.get_event_loop().run_until_complete(main())
➜  ~ ha core info
arch: amd64
audio_input: None
audio_output: None
boot: true
image: homeassistant/qemux86-64-homeassistant
ip_address: 172.30.32.1
last_version: 2020.12.1
machine: qemux86-64
port: 8123
ssl: false
update_available: true
version: 0.118.4
version_latest: 2020.12.1
wait_boot: 600
watchdog: true
➜  ~ docker exec -it homeassistant /bin/bash
bash-5.0# python myqtest.py
2020-12-26 22:23:12,469 - asyncio - DEBUG - Using selector: EpollSelector
2020-12-26 22:23:12,470 - pymyq.api - DEBUG - Sending authentication request to MyQ
2020-12-26 22:23:12,470 - pymyq.api - DEBUG - Sending myq api request https://api.myqdevice.com/api/v5/Login
2020-12-26 22:23:12,641 - pymyq.api - DEBUG - Attempt 1 request failed with exception: [Errno 104] Connection reset by peer
2020-12-26 22:23:12,641 - pymyq.api - WARNING - Device update failed; trying again in 2 seconds
2020-12-26 22:23:14,644 - pymyq.api - DEBUG - Sending myq api request https://api.myqdevice.com/api/v5/Login
2020-12-26 22:23:14,772 - pymyq.api - DEBUG - Attempt 2 request failed with exception: [Errno 104] Connection reset by peer
2020-12-26 22:23:14,772 - pymyq.api - WARNING - Device update failed; trying again in 4 seconds
^CTraceback (most recent call last):
  File "myqtest.py", line 32, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/local/lib/python3.8/selectors.py", line 468, in select
    fd_event_list = self._selector.poll(timeout, max_ev)
KeyboardInterrupt
> docker run -it homeassistant/home-assistant:stable /bin/bash
bash-5.0# touch myqtest.py
bash-5.0# vi myqtest.py
bash-5.0# python myqtest.py
2020-12-27 03:36:47,890 - asyncio - DEBUG - Using selector: EpollSelector
2020-12-27 03:36:47,891 - pymyq.api - DEBUG - Sending authentication request to MyQ
2020-12-27 03:36:47,891 - pymyq.api - DEBUG - Sending myq api request https://api.myqdevice.com/api/v5/Login
2020-12-27 03:36:48,204 - pymyq.api - DEBUG - Attempt 1 request failed with exception: Server disconnected
2020-12-27 03:36:48,204 - pymyq.api - WARNING - Device update failed; trying again in 2 seconds
^CTraceback (most recent call last):
  File "myqtest.py", line 32, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/local/lib/python3.8/selectors.py", line 468, in select
    fd_event_list = self._selector.poll(timeout, max_ev)
KeyboardInterrupt

bash-5.0#
you will notice in a tcpdump the server instantly sends a reset

➜  ~ sudo tcpdump -i ens160 host 40.71.237.40
[sudo] password for administrator:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens160, link-type EN10MB (Ethernet), capture size 262144 bytes
03:31:11.316306 IP docker.60026 > 40.71.237.40.https: Flags [S], seq 1729590167, win 64240, options [mss 1460,sackOK,TS val 1560322848 ecr 0,nop,wscale 7], length 0
03:31:11.342367 IP 40.71.237.40.https > docker.60026: Flags [S.], seq 1223256917, ack 1729590168, win 14600, options [mss 1440,nop,wscale 0,sackOK,TS val 2722085632 ecr 1560322848], length 0
03:31:11.342423 IP docker.60026 > 40.71.237.40.https: Flags [.], ack 1, win 502, options [nop,nop,TS val 1560322874 ecr 2722085632], length 0
03:31:11.343595 IP docker.60026 > 40.71.237.40.https: Flags [P.], seq 1:518, ack 1, win 502, options [nop,nop,TS val 1560322876 ecr 2722085632], length 517
03:31:11.364213 IP 40.71.237.40.https > docker.60026: Flags [.], ack 518, win 15117, options [nop,nop,TS val 2722085654 ecr 1560322876], length 0
03:31:11.365771 IP 40.71.237.40.https > docker.60026: Flags [P.], seq 1:4396, ack 518, win 15117, options [nop,nop,TS val 2722085655 ecr 1560322876], length 4395
03:31:11.365807 IP docker.60026 > 40.71.237.40.https: Flags [.], ack 4396, win 487, options [nop,nop,TS val 1560322898 ecr 2722085655], length 0
03:31:11.368139 IP docker.60026 > 40.71.237.40.https: Flags [P.], seq 518:644, ack 4396, win 501, options [nop,nop,TS val 1560322900 ecr 2722085655], length 126
03:31:11.398784 IP 40.71.237.40.https > docker.60026: Flags [.], ack 644, win 15243, options [nop,nop,TS val 2722085686 ecr 1560322900], length 0
03:31:11.398784 IP 40.71.237.40.https > docker.60026: Flags [P.], seq 4396:4447, ack 644, win 15243, options [nop,nop,TS val 2722085687 ecr 1560322900], length 51
03:31:11.398821 IP docker.60026 > 40.71.237.40.https: Flags [.], ack 4447, win 501, options [nop,nop,TS val 1560322931 ecr 2722085687], length 0
03:31:11.399956 IP docker.60026 > 40.71.237.40.https: Flags [P.], seq 644:979, ack 4447, win 501, options [nop,nop,TS val 1560322932 ecr 2722085687], length 335
03:31:11.400287 IP docker.60026 > 40.71.237.40.https: Flags [P.], seq 979:1086, ack 4447, win 501, options [nop,nop,TS val 1560322932 ecr 2722085687], length 107
03:31:11.427498 IP 40.71.237.40.https > docker.60026: Flags [.], ack 979, win 15578, options [nop,nop,TS val 2722085718 ecr 1560322932], length 0
03:31:11.427498 IP 40.71.237.40.https > docker.60026: Flags [.], ack 1086, win 15685, options [nop,nop,TS val 2722085718 ecr 1560322932], length 0
03:31:11.428320 IP 40.71.237.40.https > docker.60026: Flags [R.], seq 4447, ack 1086, win 0, length 0

Now if i use this example file it at least shows it can reach outbound


bash-5.0# python myqtest\ copy.py
Status: 401
Content-type: application/json; charset=utf-8
Body: {'code': '401.102', 'message': 'Unauthorized', 'description': 'The current call is Unauthorized.'} ...
bash-5.0#

import aiohttp
import asyncio

async def main():

    async with aiohttp.ClientSession() as session:
        async with session.request('POST', 'https://api.myqdevice.com/api/v5/Login') as response:

            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])

            html = await response.json(content_type=None)
            print("Body:", html, "...")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
dseven commented 3 years ago

You need pymyq 2.0.12. home-assistant hasn't made a release that picks it up yet.

jbronikowski commented 3 years ago

That was it. I will have to check out what the fix was in your latest release. Thanks