mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
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:
parent
789ad38c38
commit
16fff16082
@ -1,5 +1,6 @@
|
|||||||
"""Support for UV data from openuv.io."""
|
"""Support for UV data from openuv.io."""
|
||||||
import logging
|
import logging
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -198,19 +199,33 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
|
|
||||||
@_verify_domain_control
|
@_verify_domain_control
|
||||||
async def update_data(service):
|
async def update_data(service):
|
||||||
"""Refresh OpenUV data."""
|
"""Refresh all OpenUV data."""
|
||||||
_LOGGER.debug("Refreshing OpenUV data")
|
_LOGGER.debug("Refreshing all OpenUV data")
|
||||||
|
await openuv.async_update()
|
||||||
try:
|
|
||||||
await openuv.async_update()
|
|
||||||
except OpenUvError as err:
|
|
||||||
_LOGGER.error("Error during data update: %s", err)
|
|
||||||
return
|
|
||||||
|
|
||||||
async_dispatcher_send(hass, TOPIC_UPDATE)
|
async_dispatcher_send(hass, TOPIC_UPDATE)
|
||||||
|
|
||||||
hass.services.async_register(DOMAIN, "update_data", update_data)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -234,21 +249,36 @@ class OpenUV:
|
|||||||
self.data = {}
|
self.data = {}
|
||||||
self.sensor_conditions = sensor_conditions
|
self.sensor_conditions = sensor_conditions
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update_protection_data(self):
|
||||||
"""Update sensor/binary sensor data."""
|
"""Update binary sensor (protection window) data."""
|
||||||
if TYPE_PROTECTION_WINDOW in self.binary_sensor_conditions:
|
from pyopenuv.errors import OpenUvError
|
||||||
resp = await self.client.uv_protection_window()
|
|
||||||
data = resp["result"]
|
|
||||||
|
|
||||||
if data.get("from_time") and data.get("to_time"):
|
if TYPE_PROTECTION_WINDOW in self.binary_sensor_conditions:
|
||||||
self.data[DATA_PROTECTION_WINDOW] = data
|
try:
|
||||||
else:
|
resp = await self.client.uv_protection_window()
|
||||||
_LOGGER.debug("No valid protection window data for this location")
|
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] = {}
|
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):
|
if any(c in self.sensor_conditions for c in SENSORS):
|
||||||
data = await self.client.uv_index()
|
try:
|
||||||
self.data[DATA_UV] = data
|
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):
|
class OpenUvEntity(Entity):
|
||||||
|
@ -2,4 +2,10 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
update_data:
|
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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user