public static IHomeAssistantServiceCollection AddClient(this IHomeAssistantServiceCollection services, Action <IHomeAssistantClientConfigurationBuilder> builderConfigurator = null)
        {
            services.AddOptions <HomeAssistantClientOptions>()
            .Bind(services.Configuration.GetSection("client"));

            services.AddHttpClient <IHomeAssistantRestClient, HomeAssistantRestClient>((sp, client) =>
            {
                var options        = sp.GetRequiredService <IOptions <HomeAssistantClientOptions> >();
                client.BaseAddress = options.Value.ApiUrlPrefix;

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", options.Value.Auth.Token);
            });

            services.AddSingleton <IHomeAssistantWebSocketsConnectionService, HomeAssistantWebSocketsConnectionService>();
            services.AddSingleton <IHomeAssistantWebSocketsClientFactory, HomeAssistantWebSocketsClientFactory>();
            services.AddTransient <IHomeAssistantWebSocketsClient, HomeAssistantWebSocketsClient>();
            services.AddHostedService <HomeAssistantWebSocketsClientBackgroundService>();

            services.AddSingleton <IStateProvider, StateProvider>();
            services.AddSingleton <IStateCodeGenerator, NoOpStateCodeGenerator>();

            var configurationBuilder = new HomeAssistantClientConfigurationBuilder(services.Configuration, services);

            builderConfigurator?.Invoke(configurationBuilder);

            return(services);
        }
Пример #2
0
        public static IHomeAssistantServiceCollection AddMqtt(
            this IHomeAssistantServiceCollection services,
            Action <IHomeAssistantMqttConfigurationBuilder> builderConfigurator = null)
        {
            services.AddSingleton <IMqttMessageBus, MqttMessageBus>();

            services.AddSingleton <IMqttEntityControlPanel, MqttEntityControlPanel>();
            services.AddSingleton <IConfigureMqttApplicationBuilder>(sp => sp.GetRequiredService <IMqttEntityControlPanel>());

            services.AddHostedService <HomeAssistantOutboundMqttDispatcher>();

            var configurationBuilder = new HomeAssistantMqttConfigurationBuilder(services.Configuration, services);

            builderConfigurator?.Invoke(configurationBuilder);

            return(services);
        }