Address late review of ondilo_ico (#44837)
* Updates following comments in PR 44728 * Make all api calls in same thread context * Set API as parameter to get_all_pools_data * extract pools data retrieval function to api classpull/44879/head
parent
02bfc68842
commit
1a65ab0b80
|
@ -1,11 +1,14 @@
|
||||||
"""API for Ondilo ICO bound to Home Assistant OAuth."""
|
"""API for Ondilo ICO bound to Home Assistant OAuth."""
|
||||||
from asyncio import run_coroutine_threadsafe
|
from asyncio import run_coroutine_threadsafe
|
||||||
|
import logging
|
||||||
|
|
||||||
from ondilo import Ondilo
|
from ondilo import Ondilo
|
||||||
|
|
||||||
from homeassistant import config_entries, core
|
from homeassistant import config_entries, core
|
||||||
from homeassistant.helpers import config_entry_oauth2_flow
|
from homeassistant.helpers import config_entry_oauth2_flow
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class OndiloClient(Ondilo):
|
class OndiloClient(Ondilo):
|
||||||
"""Provide Ondilo ICO authentication tied to an OAuth2 based config entry."""
|
"""Provide Ondilo ICO authentication tied to an OAuth2 based config entry."""
|
||||||
|
@ -31,3 +34,17 @@ class OndiloClient(Ondilo):
|
||||||
).result()
|
).result()
|
||||||
|
|
||||||
return self.session.token
|
return self.session.token
|
||||||
|
|
||||||
|
def get_all_pools_data(self) -> dict:
|
||||||
|
"""Fetch pools and add pool details and last measures to pool data."""
|
||||||
|
|
||||||
|
pools = self.get_pools()
|
||||||
|
for pool in pools:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Retrieving data for pool/spa: %s, id: %d", pool["name"], pool["id"]
|
||||||
|
)
|
||||||
|
pool["ICO"] = self.get_ICO_details(pool["id"])
|
||||||
|
pool["sensors"] = self.get_last_pool_measures(pool["id"])
|
||||||
|
_LOGGER.debug("Retrieved the following sensors data: %s", pool["sensors"])
|
||||||
|
|
||||||
|
return pools
|
||||||
|
|
|
@ -6,9 +6,6 @@
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"ondilo==0.2.0"
|
"ondilo==0.2.0"
|
||||||
],
|
],
|
||||||
"ssdp": [],
|
|
||||||
"zeroconf": [],
|
|
||||||
"homekit": {},
|
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"http"
|
"http"
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
"""Platform for sensor integration."""
|
"""Platform for sensor integration."""
|
||||||
import asyncio
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -25,24 +24,23 @@ SENSOR_TYPES = {
|
||||||
"temperature": [
|
"temperature": [
|
||||||
"Temperature",
|
"Temperature",
|
||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
"mdi:thermometer",
|
None,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
],
|
],
|
||||||
"orp": ["Oxydo Reduction Potential", "mV", "mdi:pool", None],
|
"orp": ["Oxydo Reduction Potential", "mV", "mdi:pool", None],
|
||||||
"ph": ["pH", "", "mdi:pool", None],
|
"ph": ["pH", "", "mdi:pool", None],
|
||||||
"tds": ["TDS", CONCENTRATION_PARTS_PER_MILLION, "mdi:pool", None],
|
"tds": ["TDS", CONCENTRATION_PARTS_PER_MILLION, "mdi:pool", None],
|
||||||
"battery": ["Battery", PERCENTAGE, "mdi:battery", DEVICE_CLASS_BATTERY],
|
"battery": ["Battery", PERCENTAGE, None, DEVICE_CLASS_BATTERY],
|
||||||
"rssi": [
|
"rssi": [
|
||||||
"RSSI",
|
"RSSI",
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
"mdi:wifi-strength-2",
|
None,
|
||||||
DEVICE_CLASS_SIGNAL_STRENGTH,
|
DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
],
|
],
|
||||||
"salt": ["Salt", "mg/L", "mdi:pool", None],
|
"salt": ["Salt", "mg/L", "mdi:pool", None],
|
||||||
}
|
}
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(hours=1)
|
SCAN_INTERVAL = timedelta(hours=1)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,13 +49,6 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
|
|
||||||
api = hass.data[DOMAIN][entry.entry_id]
|
api = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
def get_all_pool_data(pool):
|
|
||||||
"""Add pool details and last measures to pool data."""
|
|
||||||
pool["ICO"] = api.get_ICO_details(pool["id"])
|
|
||||||
pool["sensors"] = api.get_last_pool_measures(pool["id"])
|
|
||||||
|
|
||||||
return pool
|
|
||||||
|
|
||||||
async def async_update_data():
|
async def async_update_data():
|
||||||
"""Fetch data from API endpoint.
|
"""Fetch data from API endpoint.
|
||||||
|
|
||||||
|
@ -65,14 +56,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
so entities can quickly look up their data.
|
so entities can quickly look up their data.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
pools = await hass.async_add_executor_job(api.get_pools)
|
return await hass.async_add_executor_job(api.get_all_pools_data)
|
||||||
|
|
||||||
return await asyncio.gather(
|
|
||||||
*[
|
|
||||||
hass.async_add_executor_job(get_all_pool_data, pool)
|
|
||||||
for pool in pools
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
except OndiloError as err:
|
except OndiloError as err:
|
||||||
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
||||||
|
@ -145,11 +129,6 @@ class OndiloICO(CoordinatorEntity):
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Last value of the sensor."""
|
"""Last value of the sensor."""
|
||||||
_LOGGER.debug(
|
|
||||||
"Retrieving Ondilo sensor %s state value: %s",
|
|
||||||
self._name,
|
|
||||||
self._devdata()["value"],
|
|
||||||
)
|
|
||||||
return self._devdata()["value"]
|
return self._devdata()["value"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow, setup
|
from homeassistant import config_entries, data_entry_flow, setup
|
||||||
from homeassistant.components.ondilo_ico import config_flow
|
|
||||||
from homeassistant.components.ondilo_ico.const import (
|
from homeassistant.components.ondilo_ico.const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
OAUTH2_AUTHORIZE,
|
OAUTH2_AUTHORIZE,
|
||||||
|
@ -23,9 +22,6 @@ async def test_abort_if_existing_entry(hass):
|
||||||
"""Check flow abort when an entry already exist."""
|
"""Check flow abort when an entry already exist."""
|
||||||
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
|
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
|
||||||
|
|
||||||
flow = config_flow.OAuth2FlowHandler()
|
|
||||||
flow.hass = hass
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue