n8henrie / fauxmo

Emulated Belkin WeMo devices that work with the Amazon Echo
https://n8henrie.com/2016/02/fauxmo-home-automation-with-the-amazon-echo-raspberry-pi-and-homeassistant/
Other
376 stars 78 forks source link

Problem getting state after turn on/iff #115

Closed gdudek closed 2 years ago

gdudek commented 2 years ago

My Issue

When I turn on my faxumo device with Alexa, I usually see an exception and then the Alex reports an ``error with the device'' (even though the device changes state correctly).

The exception is: _Exception in callback _SelectorSocketTransport._readready() which is down the call chain from _handleaction(msg)

Alex then reports one of a few errors such as that the device "isn't responding"

[solution at the bottom]

2022-07-17 11:01:36 fauxmo:140      INFO     Attempting to get state for raspberry
2022-07-17 11:01:38 asyncio:1274     ERROR    Exception in callback _SelectorSocketTransport._read_ready()
handle: <Handle _SelectorSocketTransport._read_ready() created at /usr/local/lib/python3.6/asyncio/selector_events.py:262>
source_traceback: Object created at (most recent call last):
  File "/usr/local/bin/fauxmo", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.6/site-packages/fauxmo/cli.py", line 37, in cli
    main(config_path_str=args.config, verbosity=verbosity)
  File "/usr/local/lib/python3.6/site-packages/fauxmo/fauxmo.py", line 154, in main
    loop.run_forever()
  File "/usr/local/lib/python3.6/asyncio/base_events.py", line 427, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.6/asyncio/base_events.py", line 1432, in _run_once
    handle._run()
  File "/usr/local/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 680, in _add_reader
    self._loop._add_reader(fd, callback, *args)
  File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 262, in _add_reader
    handle = events.Handle(callback, args, self)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 732, in _read_ready
    self._protocol.data_received(data)
  File "/usr/local/lib/python3.6/site-packages/fauxmo/protocols.py", line 67, in data_received
    self.handle_action(msg)
  File "/usr/local/lib/python3.6/site-packages/fauxmo/protocols.py", line 145, in handle_action
    state = self.plugin.get_state().casefold()
  File "/usr/local/lib/python3.6/site-packages/fauxmo/plugins/simplehttpplugin.py", line 196, in get_state
    with self.urlopen(req) as resp:
  File "/usr/local/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/usr/local/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.6/urllib/request.py", line 1346, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/local/lib/python3.6/urllib/request.py", line 1321, in do_open
    r = h.getresponse()
  File "/usr/local/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/local/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/local/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

I "solved this" by adding a horrifically ugly 2s sleep in get_state in simplehttpplugin.py and filed a pull request, but the "fix" is probably too ugly for most people who don't have this problem.

n8henrie commented 2 years ago

EDIT: Removing my "use the template" quick reply -- I missed it at the bottom of your post. Sorry!

n8henrie commented 2 years ago

I haven't seen this particular exception before (though I've dealt with a similar behavior in the past -- "turn off switch" ... switch turns off ... "The device is not responding" or similar error).

I also have no idea why the sleep would help anything -- out of curiosity, what made you think to try that?

On older hardware like a Pi2, I wonder if the speed of the device is becoming an issue. Have you tried installing something like uvlooop that can considerably speed up the async event loop?

From the template:

n8henrie commented 2 years ago

Bump.

This issue is pending a response and is blocked until I have more info. To keep the issue list groomed, I will often close these stalled issues if they go a prolonged time without a response. In this case, I will give it another week or so.

gdudek commented 2 years ago

Thanks for the feedback. I tried using a sleep because the error seems sporadic and thus smells a bit like a race condition to me. Putting in a short sleep worked (as opposed to other tricks I considered). My hypothesis is that Alexa and/or downstream software is not able to accept very rapid messages. If UDP is being used (and I apoglize for not remembering) then it' possible the the message could arrive out of order without the delay.

n8henrie commented 2 years ago

Fair enough. If the issue returns and/or you're able to complete the rest of the issue template let me know!