From d89c56829c7dcb73373c4e0aacd5f9327065ed60 Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Tue, 12 Feb 2019 17:15:02 +0100 Subject: [PATCH] Fix Point does I/O in event loop (#20939) * call I/O operations via hass.async_add_executor_job * asyncio fixes * Fixes from @amelchio * async _update_callback --- homeassistant/components/point/__init__.py | 20 ++++++++++--------- .../components/point/binary_sensor.py | 3 +-- homeassistant/components/point/sensor.py | 7 +++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/point/__init__.py b/homeassistant/components/point/__init__.py index 66044678e28..e0f1e6651c6 100644 --- a/homeassistant/components/point/__init__.py +++ b/homeassistant/components/point/__init__.py @@ -12,7 +12,6 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_TOKEN, CONF_WEBHOOK_ID -from homeassistant.core import callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send) @@ -117,8 +116,11 @@ async def async_setup_webhook(hass: HomeAssistantType, entry: ConfigEntry, entry, data={ **entry.data, }) - session.update_webhook(entry.data[CONF_WEBHOOK_URL], - entry.data[CONF_WEBHOOK_ID], events=['*']) + await hass.async_add_executor_job( + session.update_webhook, + entry.data[CONF_WEBHOOK_URL], + entry.data[CONF_WEBHOOK_ID], + ['*']) hass.components.webhook.async_register( DOMAIN, 'Point', entry.data[CONF_WEBHOOK_ID], handle_webhook) @@ -127,8 +129,8 @@ async def async_setup_webhook(hass: HomeAssistantType, entry: ConfigEntry, async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry): """Unload a config entry.""" hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID]) - client = hass.data[DOMAIN].pop(entry.entry_id) - client.remove_webhook() + session = hass.data[DOMAIN].pop(entry.entry_id) + await hass.async_add_executor_job(session.remove_webhook) if not hass.data[DOMAIN]: hass.data.pop(DOMAIN) @@ -174,7 +176,8 @@ class MinutPointClient(): async def _sync(self): """Update local list of devices.""" - if not self._client.update() and self._is_available: + if not await self._hass.async_add_executor_job( + self._client.update) and self._is_available: self._is_available = False _LOGGER.warning("Device is unavailable") return @@ -237,15 +240,14 @@ class MinutPointEntity(Entity): _LOGGER.debug('Created device %s', self) self._async_unsub_dispatcher_connect = async_dispatcher_connect( self.hass, SIGNAL_UPDATE_ENTITY, self._update_callback) - self._update_callback() + await self._update_callback() async def async_will_remove_from_hass(self): """Disconnect dispatcher listener when removed.""" if self._async_unsub_dispatcher_connect: self._async_unsub_dispatcher_connect() - @callback - def _update_callback(self): + async def _update_callback(self): """Update the value of the sensor.""" pass diff --git a/homeassistant/components/point/binary_sensor.py b/homeassistant/components/point/binary_sensor.py index 1bd97ce2747..5f4834894bc 100644 --- a/homeassistant/components/point/binary_sensor.py +++ b/homeassistant/components/point/binary_sensor.py @@ -75,8 +75,7 @@ class MinutPointBinarySensor(MinutPointEntity, BinarySensorDevice): if self._async_unsub_hook_dispatcher_connect: self._async_unsub_hook_dispatcher_connect() - @callback - def _update_callback(self): + async def _update_callback(self): """Update the value of the sensor.""" if not self.is_updated: return diff --git a/homeassistant/components/point/sensor.py b/homeassistant/components/point/sensor.py index eb320de0efd..902c1a6c981 100644 --- a/homeassistant/components/point/sensor.py +++ b/homeassistant/components/point/sensor.py @@ -13,7 +13,6 @@ from homeassistant.components.sensor import DOMAIN from homeassistant.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS) -from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.util.dt import parse_datetime @@ -50,12 +49,12 @@ class MinutPointSensor(MinutPointEntity): super().__init__(point_client, device_id, device_class) self._device_prop = SENSOR_TYPES[device_class] - @callback - def _update_callback(self): + async def _update_callback(self): """Update the value of the sensor.""" if self.is_updated: _LOGGER.debug('Update sensor value for %s', self) - self._value = self.device.sensor(self.device_class) + self._value = await self.hass.async_add_executor_job( + self.device.sensor, self.device_class) self._updated = parse_datetime(self.device.last_update) self.async_schedule_update_ha_state()