예제 #1
0
 public YamlApp(IHaContext ha, IAppConfig <TestConfig> config)
 {
     ha?.CallService("notify", "persistent_notification", data: new
     {
         message = $"My sensor is {config?.Value.MySensor}",
         title   = "Hello config!"
     });
 }
예제 #2
0
 public HelloApp(IHaContext ha, ILogger <HelloApp> logger)
 {
     // .Where(n => n.EventType == "test_event")
     ha?.Events.Where(n => n.EventType == "test_event").Subscribe(n =>
     {
         logger.LogInformation("Hello testevent");
     });
     ha?.CallService("notify", "persistent_notification", data: new { message = "Notify me", title = "Hello world!" });
 }
예제 #3
0
 public BasicApp(
     IHaContext haContext
     )
 {
     haContext.StateChanges()
     .Where(n =>
            n.Entity.EntityId == "input_select.who_cooks"
            )
     .Subscribe(
         s => haContext.CallService("input_text", "set_value",
                                    ServiceTarget.FromEntities("input_text.test_result"), new { value = s.New?.State })
         );
 }
예제 #4
0
    /// <summary>
    ///     Creates a service in Home Assistant and registers a callback to be invoked when the service is called
    /// </summary>
    /// <param name="haContext">IHaContext to use</param>
    /// <param name="serviceName">The name of the service to create in Home Assistant. Will be prefixes with 'NetDaemon.'</param>
    /// <param name="callBack">The Action to invoke when the Service is called</param>
    /// <typeparam name="T">Type to deserialize the payload of the service into</typeparam>
    public static void RegisterServiceCallBack <T>(this IHaContext haContext, string serviceName, Action <T> callBack)
    {
        ArgumentNullException.ThrowIfNull(haContext);
        if (string.IsNullOrWhiteSpace(serviceName))
        {
            throw new ArgumentException("serviceName must have a value", serviceName);
        }

        haContext.CallService("netdaemon", "register_service", data: new { service = serviceName });

        haContext.Events.Filter <HassServiceEventData <T> >("call_service")
        .Where(e => e.Data?.domain == "netdaemon" &&
               string.Equals(e.Data?.service, serviceName, StringComparison.OrdinalIgnoreCase))
        .Subscribe(e => callBack.Invoke(e.Data !.service_data));
    }
예제 #5
0
    /// <summary>
    ///     Set application state
    /// </summary>
    /// <param name="haContext">IHaContext to use</param>
    /// <param name="entityId">EntityId of the entity to create</param>
    /// <param name="state">Entity state</param>
    /// <param name="attributes">Entity attributes</param>
    public static void SetEntityState(this IHaContext haContext, string entityId, string state,
                                      object?attributes = null)
    {
        ArgumentNullException.ThrowIfNull(haContext);
        var currentState = haContext.GetState(entityId);
        var service      = currentState is null ? "entity_create" : "entity_update";

        // We have an integration that will help persist
        haContext.CallService("netdaemon", service,
                              data: new
        {
            entity_id = entityId,
            state,
            attributes
        });
    }
예제 #6
0
    public LocalApp(
        IHaContext ha
        )
    {
        ha.StateChanges()
        .Where(n => n.Entity.EntityId == "binary_sensor.mypir" && n.New?.State == "on")
        .Subscribe(_ =>
        {
            ha.CallService("light", "turn_on", ServiceTarget.FromEntities("light.my_light"));
        });

        ha.StateChanges()
        .Where(n => n.Entity.EntityId == "binary_sensor.mypir_creates_fault" && n.New?.State == "on")
        .Subscribe(_ =>
        {
            throw new InvalidOperationException("Ohh nooo!");
        });
    }
예제 #7
0
    public async Task BasicTestApp_ShouldChangeStateOfInputTextToTheStateOfInputSelect_WhenChange()
    {
        var optionToSet = GetDifferentOptionThanCurrentlySelected();

        var waitTask = _haContext.StateChanges()
                       .Where(n => n.Entity.EntityId == "input_text.test_result")
                       .Timeout(TimeSpan.FromMilliseconds(5000))
                       .FirstAsync()
                       .ToTask();

        _haContext.CallService(
            "input_select",
            "select_option",
            ServiceTarget.FromEntities("input_select.who_cooks"),
            new { option = optionToSet });

        var result = await waitTask.ConfigureAwait(false);

        result.New !.State.Should().Be(optionToSet);
    }
예제 #8
0
 public Task InitializeAsync()
 {
     _ha.CallService("notify", "persistent_notification", data: new { message = "Hello", title = "Yay it works via DI!" });;
     return(Task.CompletedTask);
 }