2019-04-25 03:09:01 +00:00
|
|
|
"""Support for IQVIA."""
|
2019-04-26 17:06:46 +00:00
|
|
|
import asyncio
|
2019-04-25 03:09:01 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2019-04-26 17:06:46 +00:00
|
|
|
from pyiqvia import Client
|
2020-10-17 16:16:41 +00:00
|
|
|
from pyiqvia.errors import IQVIAError
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2020-08-03 17:35:36 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2019-04-25 03:09:01 +00:00
|
|
|
from homeassistant.core import callback
|
2020-08-03 17:35:36 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
2020-10-17 16:16:41 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
UpdateFailed,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-25 03:09:01 +00:00
|
|
|
|
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ZIP_CODE,
|
2020-10-17 16:16:41 +00:00
|
|
|
DATA_COORDINATOR,
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
2020-10-17 16:16:41 +00:00
|
|
|
LOGGER,
|
2019-07-31 19:25:30 +00:00
|
|
|
TYPE_ALLERGY_FORECAST,
|
|
|
|
TYPE_ALLERGY_INDEX,
|
|
|
|
TYPE_ALLERGY_OUTLOOK,
|
|
|
|
TYPE_ASTHMA_FORECAST,
|
|
|
|
TYPE_ASTHMA_INDEX,
|
|
|
|
TYPE_DISEASE_FORECAST,
|
|
|
|
TYPE_DISEASE_INDEX,
|
|
|
|
)
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_ATTRIBUTION = "Data provided by IQVIA™"
|
2019-04-25 03:09:01 +00:00
|
|
|
DEFAULT_SCAN_INTERVAL = timedelta(minutes=30)
|
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
PLATFORMS = ["sensor"]
|
2020-03-17 05:58:50 +00:00
|
|
|
|
|
|
|
|
2019-04-25 03:09:01 +00:00
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the IQVIA component."""
|
2020-10-17 16:16:41 +00:00
|
|
|
hass.data[DOMAIN] = {DATA_COORDINATOR: {}}
|
2019-05-09 16:11:51 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
2019-05-09 16:11:51 +00:00
|
|
|
"""Set up IQVIA as config entry."""
|
2020-10-17 16:16:41 +00:00
|
|
|
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id] = {}
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
if not entry.unique_id:
|
2020-08-03 17:35:36 +00:00
|
|
|
# If the config entry doesn't already have a unique ID, set one:
|
|
|
|
hass.config_entries.async_update_entry(
|
2020-10-17 16:16:41 +00:00
|
|
|
entry, **{"unique_id": entry.data[CONF_ZIP_CODE]}
|
2020-08-03 17:35:36 +00:00
|
|
|
)
|
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
client = Client(entry.data[CONF_ZIP_CODE], websession)
|
2020-03-17 05:58:50 +00:00
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
async def async_get_data_from_api(api_coro):
|
|
|
|
"""Get data from a particular API coroutine."""
|
|
|
|
try:
|
|
|
|
return await api_coro()
|
|
|
|
except IQVIAError as err:
|
|
|
|
raise UpdateFailed from err
|
|
|
|
|
|
|
|
init_data_update_tasks = []
|
|
|
|
for sensor_type, api_coro in [
|
|
|
|
(TYPE_ALLERGY_FORECAST, client.allergens.extended),
|
|
|
|
(TYPE_ALLERGY_INDEX, client.allergens.current),
|
|
|
|
(TYPE_ALLERGY_OUTLOOK, client.allergens.outlook),
|
|
|
|
(TYPE_ASTHMA_FORECAST, client.asthma.extended),
|
|
|
|
(TYPE_ASTHMA_INDEX, client.asthma.current),
|
|
|
|
(TYPE_DISEASE_FORECAST, client.disease.extended),
|
|
|
|
(TYPE_DISEASE_INDEX, client.disease.current),
|
|
|
|
]:
|
|
|
|
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id][
|
|
|
|
sensor_type
|
|
|
|
] = DataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
name=f"{entry.data[CONF_ZIP_CODE]} {sensor_type}",
|
|
|
|
update_interval=DEFAULT_SCAN_INTERVAL,
|
|
|
|
update_method=lambda coro=api_coro: async_get_data_from_api(coro),
|
|
|
|
)
|
|
|
|
init_data_update_tasks.append(coordinator.async_refresh())
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
await asyncio.gather(*init_data_update_tasks)
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
for component in PLATFORMS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2019-05-09 16:11:51 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
async def async_unload_entry(hass, entry):
|
2019-05-09 16:11:51 +00:00
|
|
|
"""Unload an OpenUV config entry."""
|
2020-10-17 16:16:41 +00:00
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in PLATFORMS
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN][DATA_COORDINATOR].pop(entry.entry_id)
|
2020-03-17 05:58:50 +00:00
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
return unload_ok
|
2020-03-17 05:58:50 +00:00
|
|
|
|
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
class IQVIAEntity(CoordinatorEntity):
|
2019-04-25 03:09:01 +00:00
|
|
|
"""Define a base IQVIA entity."""
|
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
def __init__(self, coordinator, entry, sensor_type, name, icon):
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(coordinator)
|
2019-04-25 03:09:01 +00:00
|
|
|
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
2020-10-17 16:16:41 +00:00
|
|
|
self._entry = entry
|
2019-04-25 03:09:01 +00:00
|
|
|
self._icon = icon
|
|
|
|
self._name = name
|
|
|
|
self._state = None
|
2019-04-26 17:06:46 +00:00
|
|
|
self._type = sensor_type
|
2019-04-25 03:09:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
|
|
|
return self._attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2020-10-17 16:16:41 +00:00
|
|
|
return f"{self._entry.data[CONF_ZIP_CODE]}_{self._type}"
|
2019-04-25 03:09:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "index"
|
2019-04-25 03:09:01 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-04-25 03:09:01 +00:00
|
|
|
@callback
|
|
|
|
def update():
|
|
|
|
"""Update the state."""
|
2020-03-17 05:58:50 +00:00
|
|
|
self.update_from_latest_data()
|
|
|
|
self.async_write_ha_state()
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2020-10-17 16:16:41 +00:00
|
|
|
self.async_on_remove(self.coordinator.async_add_listener(update))
|
2019-04-25 03:09:01 +00:00
|
|
|
|
2020-03-17 05:58:50 +00:00
|
|
|
if self._type == TYPE_ALLERGY_FORECAST:
|
2020-10-17 16:16:41 +00:00
|
|
|
outlook_coordinator = self.hass.data[DOMAIN][DATA_COORDINATOR][
|
|
|
|
self._entry.entry_id
|
|
|
|
][TYPE_ALLERGY_OUTLOOK]
|
|
|
|
self.async_on_remove(outlook_coordinator.async_add_listener(update))
|
2020-03-17 05:58:50 +00:00
|
|
|
|
|
|
|
self.update_from_latest_data()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self):
|
2020-10-17 16:16:41 +00:00
|
|
|
"""Update the entity from the latest data."""
|
|
|
|
raise NotImplementedError
|