* Remove unnecessary exception re-wraps * Preserve exception chains on re-raise We slap "from cause" to almost all possible cases here. In some cases it could conceivably be better to do "from None" if we really want to hide the cause. However those should be in the minority, and "from cause" should be an improvement over the corresponding raise without a "from" in all cases anyway. The only case where we raise from None here is in plex, where the exception for an original invalid SSL cert is not the root cause for failure to validate a newly fetched one. Follow local convention on exception variable names if there is a consistent one, otherwise `err` to match with majority of codebase. * Fix mistaken re-wrap in homematicip_cloud/hap.py Missed the difference between HmipConnectionError and HmipcConnectionError. * Do not hide original error on plex new cert validation error Original is not the cause for the new one, but showing old in the traceback is useful nevertheless.
100 lines
2.8 KiB
Python
100 lines
2.8 KiB
Python
"""The Dexcom integration."""
|
|
import asyncio
|
|
from datetime import timedelta
|
|
import logging
|
|
|
|
from pydexcom import AccountError, Dexcom, SessionError
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_PASSWORD, CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
from .const import (
|
|
CONF_SERVER,
|
|
COORDINATOR,
|
|
DOMAIN,
|
|
MG_DL,
|
|
PLATFORMS,
|
|
SERVER_OUS,
|
|
UNDO_UPDATE_LISTENER,
|
|
)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=180)
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
"""Set up configured Dexcom."""
|
|
hass.data[DOMAIN] = {}
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
"""Set up Dexcom from a config entry."""
|
|
try:
|
|
dexcom = await hass.async_add_executor_job(
|
|
Dexcom,
|
|
entry.data[CONF_USERNAME],
|
|
entry.data[CONF_PASSWORD],
|
|
entry.data[CONF_SERVER] == SERVER_OUS,
|
|
)
|
|
except AccountError:
|
|
return False
|
|
except SessionError as error:
|
|
raise ConfigEntryNotReady from error
|
|
|
|
if not entry.options:
|
|
hass.config_entries.async_update_entry(
|
|
entry, options={CONF_UNIT_OF_MEASUREMENT: MG_DL}
|
|
)
|
|
|
|
async def async_update_data():
|
|
try:
|
|
return await hass.async_add_executor_job(dexcom.get_current_glucose_reading)
|
|
except SessionError as error:
|
|
raise UpdateFailed(error) from error
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
COORDINATOR: DataUpdateCoordinator(
|
|
hass,
|
|
_LOGGER,
|
|
name=DOMAIN,
|
|
update_method=async_update_data,
|
|
update_interval=SCAN_INTERVAL,
|
|
),
|
|
UNDO_UPDATE_LISTENER: entry.add_update_listener(update_listener),
|
|
}
|
|
|
|
await hass.data[DOMAIN][entry.entry_id][COORDINATOR].async_refresh()
|
|
|
|
for component in PLATFORMS:
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
"""Unload a config entry."""
|
|
unload_ok = all(
|
|
await asyncio.gather(
|
|
*[
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
for component in PLATFORMS
|
|
]
|
|
)
|
|
)
|
|
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
|
|
|
|
if unload_ok:
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
return unload_ok
|
|
|
|
|
|
async def update_listener(hass, entry):
|
|
"""Handle options update."""
|
|
await hass.config_entries.async_reload(entry.entry_id)
|