homeassistant-config/custom_components/ondilo/__init__.py

79 lines
2.1 KiB
Python
Raw Normal View History

import asyncio
import logging
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
from . import config_flow
from .const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
}
)
},
extra=vol.ALLOW_EXTRA,
)
PLATFORMS = ["sensor"]
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the ondilo component."""
hass.data[DOMAIN] = {}
print("Init1")
if DOMAIN not in config:
print("Init early out")
return True
config_flow.OndiloFlowHandler.async_register_implementation(
hass,
config_entry_oauth2_flow.LocalOAuth2Implementation(
hass,
DOMAIN,
config[DOMAIN][CONF_USERNAME],
config[DOMAIN][CONF_PASSWORD],
OAUTH2_AUTHORIZE,
OAUTH2_TOKEN,
),
)
print("init finish")
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up ondilo from a config entry."""
implementation = await config_entry_oauth2_flow.async_get_config_entry_implementation(
hass, entry
)
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
hass.data[DOMAIN] = {"session": session}
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "sensor"))
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
]
)
)
if unload_ok:
hass.data[DOMAIN] = {}
return unload_ok