Add two new methods to the OpenUV component that consume only a singl… (#26207)

* 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
This commit is contained in:
StephenWetzel 2019-08-29 11:56:12 -04:00 committed by Aaron Bach
parent 789ad38c38
commit 16fff16082
2 changed files with 57 additions and 21 deletions

View file

@ -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):

View file

@ -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.