From 16fff16082b42f3b1d672c005737d137a8360478 Mon Sep 17 00:00:00 2001 From: StephenWetzel Date: Thu, 29 Aug 2019 11:56:12 -0400 Subject: [PATCH] =?UTF-8?q?Add=20two=20new=20methods=20to=20the=20OpenUV?= =?UTF-8?q?=20component=20that=20consume=20only=20a=20singl=E2=80=A6=20(#2?= =?UTF-8?q?6207)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add two new methods to the OpenUV component that consume only a single API call * Two lines after class * Rename methods to better reflect what they do, and DRY copy and pasted code * More error handling down into methods, run api calls in parallel * Fix import order * Add new methods to services.yaml, and update error messages --- homeassistant/components/openuv/__init__.py | 70 +++++++++++++------ homeassistant/components/openuv/services.yaml | 8 ++- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/openuv/__init__.py b/homeassistant/components/openuv/__init__.py index 339b8900049..c1a8873b9e0 100644 --- a/homeassistant/components/openuv/__init__.py +++ b/homeassistant/components/openuv/__init__.py @@ -1,5 +1,6 @@ """Support for UV data from openuv.io.""" import logging +import asyncio import voluptuous as vol @@ -198,19 +199,33 @@ async def async_setup_entry(hass, config_entry): @_verify_domain_control async def update_data(service): - """Refresh OpenUV data.""" - _LOGGER.debug("Refreshing OpenUV data") - - try: - await openuv.async_update() - except OpenUvError as err: - _LOGGER.error("Error during data update: %s", err) - return - + """Refresh all OpenUV data.""" + _LOGGER.debug("Refreshing all OpenUV data") + await openuv.async_update() async_dispatcher_send(hass, TOPIC_UPDATE) hass.services.async_register(DOMAIN, "update_data", update_data) + @_verify_domain_control + async def update_uv_index_data(service): + """Refresh OpenUV UV index data.""" + _LOGGER.debug("Refreshing OpenUV UV index data") + await openuv.async_update_uv_index_data() + async_dispatcher_send(hass, TOPIC_UPDATE) + + hass.services.async_register(DOMAIN, "update_uv_index_data", update_uv_index_data) + + @_verify_domain_control + async def update_protection_data(service): + """Refresh OpenUV protection window data.""" + _LOGGER.debug("Refreshing OpenUV protection window data") + await openuv.async_update_protection_data() + async_dispatcher_send(hass, TOPIC_UPDATE) + + hass.services.async_register( + DOMAIN, "update_protection_data", update_protection_data + ) + return True @@ -234,21 +249,36 @@ class OpenUV: self.data = {} self.sensor_conditions = sensor_conditions - async def async_update(self): - """Update sensor/binary sensor data.""" - if TYPE_PROTECTION_WINDOW in self.binary_sensor_conditions: - resp = await self.client.uv_protection_window() - data = resp["result"] + async def async_update_protection_data(self): + """Update binary sensor (protection window) data.""" + from pyopenuv.errors import OpenUvError - if data.get("from_time") and data.get("to_time"): - self.data[DATA_PROTECTION_WINDOW] = data - else: - _LOGGER.debug("No valid protection window data for this location") + if TYPE_PROTECTION_WINDOW in self.binary_sensor_conditions: + try: + resp = await self.client.uv_protection_window() + self.data[DATA_PROTECTION_WINDOW] = resp["result"] + except OpenUvError as err: + _LOGGER.error("Error during protection data update: %s", err) self.data[DATA_PROTECTION_WINDOW] = {} + return + + async def async_update_uv_index_data(self): + """Update sensor (uv index, etc) data.""" + from pyopenuv.errors import OpenUvError if any(c in self.sensor_conditions for c in SENSORS): - data = await self.client.uv_index() - self.data[DATA_UV] = data + try: + data = await self.client.uv_index() + self.data[DATA_UV] = data + except OpenUvError as err: + _LOGGER.error("Error during uv index data update: %s", err) + self.data[DATA_UV] = {} + return + + async def async_update(self): + """Update sensor/binary sensor data.""" + tasks = [self.async_update_protection_data(), self.async_update_uv_index_data()] + await asyncio.gather(*tasks) class OpenUvEntity(Entity): diff --git a/homeassistant/components/openuv/services.yaml b/homeassistant/components/openuv/services.yaml index f353c7f4774..be9a7ba522f 100644 --- a/homeassistant/components/openuv/services.yaml +++ b/homeassistant/components/openuv/services.yaml @@ -2,4 +2,10 @@ --- update_data: - description: Request new data from OpenUV. + description: Request new data from OpenUV. Consumes two API calls. + +update_uv_index_data: + description: Request new UV index data from OpenUV. + +update_protection_data: + description: Request new protection window data from OpenUV.