Fix Nest async from sync (#15997)

This commit is contained in:
Paulus Schoutsen 2018-08-16 13:46:43 +02:00 committed by GitHub
parent b682e48e12
commit 83b0ef4e26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,10 +4,10 @@ Support for Nest devices.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/nest/ https://home-assistant.io/components/nest/
""" """
from concurrent.futures import ThreadPoolExecutor
import logging import logging
import socket import socket
from datetime import datetime, timedelta from datetime import datetime, timedelta
import threading
import voluptuous as vol import voluptuous as vol
@ -16,8 +16,9 @@ from homeassistant.const import (
CONF_STRUCTURE, CONF_FILENAME, CONF_BINARY_SENSORS, CONF_SENSORS, CONF_STRUCTURE, CONF_FILENAME, CONF_BINARY_SENSORS, CONF_SENSORS,
CONF_MONITORED_CONDITIONS, CONF_MONITORED_CONDITIONS,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP) EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send, \ from homeassistant.helpers.dispatcher import dispatcher_send, \
async_dispatcher_connect async_dispatcher_connect
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
@ -71,24 +72,25 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
async def async_nest_update_event_broker(hass, nest): def nest_update_event_broker(hass, nest):
""" """
Dispatch SIGNAL_NEST_UPDATE to devices when nest stream API received data. Dispatch SIGNAL_NEST_UPDATE to devices when nest stream API received data.
nest.update_event.wait will block the thread in most of time, Runs in its own thread.
so specific an executor to save default thread pool.
""" """
_LOGGER.debug("listening nest.update_event") _LOGGER.debug("listening nest.update_event")
with ThreadPoolExecutor(max_workers=1) as executor:
while True: while hass.is_running:
await hass.loop.run_in_executor(executor, nest.update_event.wait) nest.update_event.wait()
if hass.is_running:
if not hass.is_running:
break
nest.update_event.clear() nest.update_event.clear()
_LOGGER.debug("dispatching nest data update") _LOGGER.debug("dispatching nest data update")
async_dispatcher_send(hass, SIGNAL_NEST_UPDATE) dispatcher_send(hass, SIGNAL_NEST_UPDATE)
else:
_LOGGER.debug("stop listening nest.update_event") _LOGGER.debug("stop listening nest.update_event")
return
async def async_setup(hass, config): async def async_setup(hass, config):
@ -167,15 +169,20 @@ async def async_setup_entry(hass, entry):
hass.services.async_register( hass.services.async_register(
DOMAIN, 'set_mode', set_mode, schema=AWAY_SCHEMA) DOMAIN, 'set_mode', set_mode, schema=AWAY_SCHEMA)
@callback
def start_up(event): def start_up(event):
"""Start Nest update event listener.""" """Start Nest update event listener."""
hass.async_add_job(async_nest_update_event_broker, hass, nest) threading.Thread(
name='Nest update listener',
target=nest_update_event_broker,
args=(hass, nest)
).start()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_up) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_up)
@callback
def shut_down(event): def shut_down(event):
"""Stop Nest update event listener.""" """Stop Nest update event listener."""
if nest:
nest.update_event.set() nest.update_event.set()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shut_down) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shut_down)