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.