8000 Nest: Fix doing async from sync by balloob · Pull Request #15997 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Nest: Fix doing async from sync #15997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions homeassistant/components/nest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
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

Expand All @@ -16,8 +16,9 @@
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

Expand Down Expand Up @@ -71,24 +72,25 @@
}, 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):
Expand Down Expand Up @@ -167,16 +169,21 @@ def set_mode(service):
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)

Expand Down
0