Add Config Flow for sense (#32160)

* Config Flow for sense

* Fix unique ids so they are actually unique (and migrate the old ones)

* Fix missing solar production

* Do not mark sensors available until they have data

* Address review items

* Address review round #2
This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-02-25 16:37:41 -07:00
1 parent b0fdbebd56
commit 3e702c8ca4
12 files changed
+367 -44

No files matched your search

+82 -23
View File
@@ -1,4 +1,5 @@
"""Support for monitoring a Sense energy sensor."""
import asyncio
from datetime import timedelta
import logging
@@ -9,21 +10,25 @@ from sense_energy import (
)
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from .const import (
ACTIVE_UPDATE_RATE,
DEFAULT_TIMEOUT,
DOMAIN,
SENSE_DATA,
SENSE_DEVICE_UPDATE,
)
_LOGGER = logging.getLogger(__name__)
ACTIVE_UPDATE_RATE = 60
DEFAULT_TIMEOUT = 5
DOMAIN = "sense"
SENSE_DATA = "sense_data"
SENSE_DEVICE_UPDATE = "sense_devices_update"
PLATFORMS = ["sensor", "binary_sensor"]
CONFIG_SCHEMA = vol.Schema(
{
@@ -39,34 +44,88 @@ CONFIG_SCHEMA = vol.Schema(
)
async def async_setup(hass, config):
"""Set up the Sense sensor."""
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Sense component."""
hass.data.setdefault(DOMAIN, {})
conf = config.get(DOMAIN)
if not conf:
return True
username = config[DOMAIN][CONF_EMAIL]
password = config[DOMAIN][CONF_PASSWORD]
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={
CONF_EMAIL: conf[CONF_EMAIL],
CONF_PASSWORD: conf[CONF_PASSWORD],
CONF_TIMEOUT: conf.get[CONF_TIMEOUT],
},
)
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up Sense from a config entry."""
entry_data = entry.data
email = entry_data[CONF_EMAIL]
password = entry_data[CONF_PASSWORD]
timeout = entry_data[CONF_TIMEOUT]
gateway = ASyncSenseable(api_timeout=timeout, wss_timeout=timeout)
gateway.rate_limit = ACTIVE_UPDATE_RATE
timeout = config[DOMAIN][CONF_TIMEOUT]
try:
hass.data[SENSE_DATA] = ASyncSenseable(api_timeout=timeout, wss_timeout=timeout)
hass.data[SENSE_DATA].rate_limit = ACTIVE_UPDATE_RATE
await hass.data[SENSE_DATA].authenticate(username, password)
await gateway.authenticate(email, password)
except SenseAuthenticationException:
_LOGGER.error("Could not authenticate with sense server")
return False
hass.async_create_task(async_load_platform(hass, "sensor", DOMAIN, {}, config))
hass.async_create_task(
async_load_platform(hass, "binary_sensor", DOMAIN, {}, config)
)
except SenseAPITimeoutException:
raise ConfigEntryNotReady
hass.data[DOMAIN][entry.entry_id] = {SENSE_DATA: gateway}
for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
)
async def async_sense_update(now):
"""Retrieve latest state."""
try:
await hass.data[SENSE_DATA].update_realtime()
async_dispatcher_send(hass, SENSE_DEVICE_UPDATE)
gateway = hass.data[DOMAIN][entry.entry_id][SENSE_DATA]
await gateway.update_realtime()
async_dispatcher_send(
hass, f"{SENSE_DEVICE_UPDATE}-{gateway.sense_monitor_id}"
)
except SenseAPITimeoutException:
_LOGGER.error("Timeout retrieving data")
async_track_time_interval(
hass.data[DOMAIN][entry.entry_id][
"track_time_remove_callback"
] = async_track_time_interval(
hass, async_sense_update, timedelta(seconds=ACTIVE_UPDATE_RATE)
)
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
]
)
)
track_time_remove_callback = hass.data[DOMAIN][entry.entry_id][
"track_time_remove_callback"
]
track_time_remove_callback()
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok