From 2fb88eb0b2eedaee3d4068c8b3449929a2fe7b01 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 23 Oct 2016 23:20:49 -0700 Subject: [PATCH] More callback annotations and naming fixes --- homeassistant/components/emulated_hue.py | 36 +++++++++---------- homeassistant/components/frontend/__init__.py | 7 ++-- homeassistant/components/sensor/torque.py | 1 + homeassistant/remote.py | 12 ++++--- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/emulated_hue.py b/homeassistant/components/emulated_hue.py index 8ae8846e44956..62680d84d3663 100644 --- a/homeassistant/components/emulated_hue.py +++ b/homeassistant/components/emulated_hue.py @@ -86,30 +86,21 @@ def setup(hass, yaml_config): upnp_listener = UPNPResponderThread( config.host_ip_addr, config.listen_port) - # @core.callback - def start_emulated_hue_bridge(event): - """Start the emulated hue bridge.""" - # hass.loop.create_task(server.start()) - # Temp, while fixing listen_once - from homeassistant.util.async import run_coroutine_threadsafe - - run_coroutine_threadsafe(server.start(), hass.loop).result() - - upnp_listener.start() - - hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_emulated_hue_bridge) - - # @core.callback + @core.callback def stop_emulated_hue_bridge(event): """Stop the emulated hue bridge.""" upnp_listener.stop() - # hass.loop.create_task(server.stop()) - # Temp, while fixing listen_once - from homeassistant.util.async import run_coroutine_threadsafe + hass.loop.create_task(server.stop()) - run_coroutine_threadsafe(server.stop(), hass.loop).result() + @core.callback + def start_emulated_hue_bridge(event): + """Start the emulated hue bridge.""" + hass.loop.create_task(server.start()) + upnp_listener.start() + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, + stop_emulated_hue_bridge) - hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_emulated_hue_bridge) + hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_emulated_hue_bridge) return True @@ -269,6 +260,7 @@ def put(self, request, username, entity_id=None): result = yield from self.async_put_light_state(json_data, entity_id) return result + @core.callback def async_get_lights_list(self): """Process a request to get the list of available lights.""" json_response = {} @@ -279,6 +271,7 @@ def async_get_lights_list(self): return self.json(json_response) + @core.callback def async_get_light_state(self, entity_id): """Process a request to get the state of an individual light.""" entity = self.hass.states.get(entity_id) @@ -353,7 +346,10 @@ def async_put_light_state(self, request_json, entity_id): return self.json(json_response) def is_entity_exposed(self, entity): - """Determine if an entity should be exposed on the emulated bridge.""" + """Determine if an entity should be exposed on the emulated bridge. + + Async friendly. + """ config = self.config if entity.attributes.get('view') is not None: diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index 43223f89932eb..494e3aee4014e 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -7,6 +7,7 @@ from aiohttp import web +from homeassistant.core import callback from homeassistant.const import EVENT_HOMEASSISTANT_START from homeassistant.components import api from homeassistant.components.http import HomeAssistantView @@ -168,7 +169,7 @@ class BootstrapView(HomeAssistantView): url = "/api/bootstrap" name = "api:bootstrap" - @asyncio.coroutine + @callback def get(self, request): """Return all data needed to bootstrap Home Assistant.""" return self.json({ @@ -230,10 +231,12 @@ def get(self, request, entity_id=None): no_auth = 'true' icons_url = '/static/mdi-{}.html'.format(FINGERPRINTS['mdi.html']) - template = self.templates.get_template('index.html') + template = yield from self.hass.loop.run_in_executor( + None, self.templates.get_template, 'index.html') # pylint is wrong # pylint: disable=no-member + # This is a jinja2 template, not a HA template so we call 'render'. resp = template.render( core_url=core_url, ui_url=ui_url, no_auth=no_auth, icons_url=icons_url, icons=FINGERPRINTS['mdi.html'], diff --git a/homeassistant/components/sensor/torque.py b/homeassistant/components/sensor/torque.py index 1961c4cd6bed5..c1cb0cd98ca2c 100644 --- a/homeassistant/components/sensor/torque.py +++ b/homeassistant/components/sensor/torque.py @@ -143,6 +143,7 @@ def icon(self): """Return the default icon of the sensor.""" return 'mdi:car' + @callback def async_on_update(self, value): """Receive an update.""" self._state = value diff --git a/homeassistant/remote.py b/homeassistant/remote.py index 6f23ffb83a6a2..ce20eb4ce0d41 100644 --- a/homeassistant/remote.py +++ b/homeassistant/remote.py @@ -213,21 +213,23 @@ def __init__(self, hass, restrict_origin=None): self._targets = {} self._lock = threading.Lock() - self._unsub_listener = None + self._async_unsub_listener = None + @ha.callback def async_connect(self, api): """Attach to a Home Assistant instance and forward events. Will overwrite old target if one exists with same host/port. """ - if self._unsub_listener is None: - self._unsub_listener = self.hass.bus.async_listen( + if self._async_unsub_listener is None: + self._async_unsub_listener = self.hass.bus.async_listen( ha.MATCH_ALL, self._event_listener) key = (api.host, api.port) self._targets[key] = api + @ha.callback def async_disconnect(self, api): """Remove target from being forwarded to.""" key = (api.host, api.port) @@ -236,8 +238,8 @@ def async_disconnect(self, api): if len(self._targets) == 0: # Remove event listener if no forwarding targets present - self._unsub_listener() - self._unsub_listener = None + self._async_unsub_listener() + self._async_unsub_listener = None return did_remove