Example #1
0
    private async Task OnHomeAssistantClientConnected(
        IHomeAssistantConnection haConnection,
        CancellationToken cancelToken
        )
    {
        try
        {
            InternalConnection = haConnection;

            _logger.LogInformation("Successfully connected to Home Assistant");
            if (!string.IsNullOrEmpty(_locationSettings.Value.ApplicationConfigurationFolder))
            {
                _logger.LogDebug("Loading applications from folder {Path}",
                                 Path.GetFullPath(_locationSettings.Value.ApplicationConfigurationFolder));
            }
            else
            {
                _logger.LogDebug("Loading applications with no configuration folder");
            }

            await _cacheManager.InitializeAsync(cancelToken).ConfigureAwait(false);

            _applicationModelContext =
                await _appModel.InitializeAsync(CancellationToken.None).ConfigureAwait(false);

            // Handle state change for apps if registered
            var appStateHandler = _serviceProvider.GetRequiredService <IHandleHomeAssistantAppStateUpdates>();
            await appStateHandler.InitializeAsync(haConnection, _applicationModelContext);
        }
        catch (Exception e)
        {
            _logger.LogError(e, "   Failed to initialize apps");
            throw;
        }
    }