hass-reos-integration/custom_components/reos_integration/__init__.py

52 lines
1.8 KiB
Python
Executable File

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.const import Platform
import logging
from .reos_api import ReosApi
from .coordinator import ReosCoordinator
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, entry: ConfigEntry) -> bool:
logging.info(msg="Reos async_setup")
hass.data[DOMAIN] = {}
# Return boolean to indicate that initialization was successful.
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
logging.info(msg="Reos async_setup_entry")
host = entry.data["host"]
username = entry.data["username"]
password = entry.data["password"]
client_id = entry.data["client_id"]
client_secret = entry.data["client_secret"]
access_token = None
if "access_token" in entry.data:
access_token = entry.data["access_token"]
refresh_token = None
if "refresh_token" in entry.data:
refresh_token = entry.data["refresh_token"]
api = ReosApi(host, username, password, client_id, client_secret, access_token, refresh_token)
async def __update_entry(access, refresh):
new = {**entry.data}
new["access_token"] = access
new["refresh_token"] = refresh
hass.config_entries.async_update_entry(entry, data=new)
api.set_update_token_callback(__update_entry)
hass.data[DOMAIN][username] = {}
hass.data[DOMAIN][username]["api"] = api
coordinator = ReosCoordinator(hass, api)
hass.data[DOMAIN][username]["coordinator"] = coordinator
await coordinator.async_config_entry_first_refresh()
await hass.config_entries.async_forward_entry_setups(entry, [Platform.BUTTON, Platform.SENSOR, Platform.BINARY_SENSOR])
return True