Skip to content
Snippets Groups Projects
Commit 48dfdde7 authored by jkerdreu's avatar jkerdreu
Browse files

Fix the delay issue w/ asyncio.



git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2709 b32b6428-25c9-4566-ad07-03861ab6144f
parent 9f30d18d
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,13 @@ from fauxmo.plugins import FauxmoPlugin
from xaal.monitor import Monitor
import time
import asyncio
import nest_asyncio
nest_asyncio.apply()
logger = logging.getLogger()
monitor = None
......@@ -65,9 +72,30 @@ class XAALPlugin(FauxmoPlugin):
send(dev.address,tmp[1])
return True
def get_state(self):
# Alexa send a bunch (3) + (2) get_state request without any delay
# Due to async use in Fauxmo, we need to async.sleep() to receive
# device update throught the monitor..
# but the get_state is not async :( .. so no await or waitfor..
# Only ensure_future() and run_until ..
# Last important notice : Python 3.9 avoid the run_until_complete
# while loop is already running. nest-asyncio come to rescue !
loop = asyncio.get_event_loop()
loop.run_until_complete(self._get_state())
return self.value
async def _get_state(self):
# we wait for 0.2 sec if the value isn't what we expected
# this let's the device (and monitor) to push new value for
# the next call
self.value = self.__get_state()
if self.value != self._latest_action:
logger.warning(f'state={self.value} != latest_action={self._latest_action}')
await asyncio.sleep(0.2)
def __get_state(self):
"loops throught devices to find a least one w/ the right state"
for dev in self.get_devices():
tmp = self.get_mapping(dev)
if tmp == None: continue
......@@ -81,7 +109,7 @@ class XAALPlugin(FauxmoPlugin):
return self._latest_action
# no device found with the right state, so send
# the current state instead. sending "unknown"
# the wrong state instead. sending "unknown"
# should be better, but it raise a big warning
if self._latest_action == 'on':
return 'off'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment