1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00

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
https://home-assistant.io/components/nest/
"""
from concurrent.futures import ThreadPoolExecutor
import logging
import socket
from datetime import datetime, timedelta
import threading
import voluptuous as vol
@ -16,8 +16,9 @@ from homeassistant.const import (
CONF_STRUCTURE, CONF_FILENAME, CONF_BINARY_SENSORS, CONF_SENSORS,
CONF_MONITORED_CONDITIONS,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
from homeassistant.core import callback
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
from homeassistant.helpers.entity import Entity
@ -71,24 +72,25 @@ CONFIG_SCHEMA = vol.Schema({
}, 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.
nest.update_event.wait will block the thread in most of time,
so specific an executor to save default thread pool.
Runs in its own thread.
"""
_LOGGER.debug("listening nest.update_event")
with ThreadPoolExecutor(max_workers=1) as executor:
while True:
await hass.loop.run_in_executor(executor, nest.update_event.wait)
if hass.is_running:
nest.update_event.clear()
_LOGGER.debug("dispatching nest data update")
async_dispatcher_send(hass, SIGNAL_NEST_UPDATE)
else:
_LOGGER.debug("stop listening nest.update_event")
return
while hass.is_running:
nest.update_event.wait()
if not hass.is_running:
break
nest.update_event.clear()
_LOGGER.debug("dispatching nest data update")
dispatcher_send(hass, SIGNAL_NEST_UPDATE)
_LOGGER.debug("stop listening nest.update_event")
async def async_setup(hass, config):
@ -167,16 +169,21 @@ async def async_setup_entry(hass, entry):
hass.services.async_register(
DOMAIN, 'set_mode', set_mode, schema=AWAY_SCHEMA)
@callback
def start_up(event):
"""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)
@callback
def shut_down(event):
"""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)