async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Risco from a config entry.""" data = entry.data risco = RiscoAPI(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN]) try: await risco.login(async_get_clientsession(hass)) except CannotConnectError as error: raise ConfigEntryNotReady() from error except UnauthorizedError: _LOGGER.exception("Failed to login to Risco cloud") return False scan_interval = entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) coordinator = RiscoDataUpdateCoordinator(hass, risco, scan_interval) await coordinator.async_refresh() events_coordinator = RiscoEventsDataUpdateCoordinator( hass, risco, entry.entry_id, 60) undo_listener = entry.add_update_listener(_update_listener) hass.data[DOMAIN][entry.entry_id] = { DATA_COORDINATOR: coordinator, UNDO_UPDATE_LISTENER: undo_listener, EVENTS_COORDINATOR: events_coordinator, } for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component)) return True
async def validate_input(hass: core.HomeAssistant, data): """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. """ risco = RiscoAPI(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN]) try: await risco.login(async_get_clientsession(hass)) finally: await risco.close() return {"title": risco.site_name}
async def async_setup_entry(opp: OpenPeerPower, entry: ConfigEntry): """Set up Risco from a config entry.""" data = entry.data risco = RiscoAPI(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN]) try: await risco.login(async_get_clientsession(opp)) except CannotConnectError as error: raise ConfigEntryNotReady() from error except UnauthorizedError: _LOGGER.exception("Failed to login to Risco cloud") return False scan_interval = entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) coordinator = RiscoDataUpdateCoordinator(opp, risco, scan_interval) await coordinator.async_config_entry_first_refresh() events_coordinator = RiscoEventsDataUpdateCoordinator( opp, risco, entry.entry_id, 60 ) undo_listener = entry.add_update_listener(_update_listener) opp.data.setdefault(DOMAIN, {}) opp.data[DOMAIN][entry.entry_id] = { DATA_COORDINATOR: coordinator, UNDO_UPDATE_LISTENER: undo_listener, EVENTS_COORDINATOR: events_coordinator, } async def start_platforms(): await asyncio.gather( *[ opp.config_entries.async_forward_entry_setup(entry, platform) for platform in PLATFORMS ] ) await events_coordinator.async_refresh() opp.async_create_task(start_platforms()) return True