Esempio n. 1
0
        public M3App(IHaContext ha)
        {
            Ha = ha;

            // Ha.CallService("notify", "persistent_notification", new { message = "Hello", title = "Yay it works in HassModel via HaContext" }, true);;

            _climateEntity = new ClimateEntity(ha, "climate.dummy_thermostat");

            _climateEntity.StateAllChanges().Where(e => e.New?.Attributes?.Temperature > 20).Subscribe();
            _climateEntity.StateAllChanges().Subscribe(OnNext);

            string?  state       = _climateEntity.State;
            string?  state2      = _climateEntity.EntityState?.State; // is the same
            DateTime?lastChanged = _climateEntity.EntityState?.LastChanged;

            Ha.StateChanges().Subscribe(e =>
            {
            });

            // Entity that has not changed yet is retrieved on demand
            var zone = new ZoneEntity(Ha, "zone.home");

            var lat = zone.Attributes?.latitude;

            var netEnergySensor = new NumericSensorEntity(Ha, "sensor.netto_energy");
            // NumericSensor has double? as state
            double?netEnergy  = netEnergySensor.State;
            double?netEnergy2 = netEnergySensor.EntityState?.State;

            netEnergySensor.StateChanges().Subscribe(e =>
                                                     Console.WriteLine($"{e.New?.Attributes?.FriendlyName} {e.New?.State} {e.New?.Attributes?.UnitOfMeasurement}"));

            // Prints: 'Netto energy 8908.81 kWh'
        }
Esempio n. 2
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!");
        });
    }
Esempio n. 3
0
 public ConcurrencyTestApp2(IHaContext context, ILogger <ConcurrencyTestApp2> logger)
 {
     context.StateChanges()
     .Where(n => n.Entity.EntityId == "input_select.who_cooks")
     .SubscribeAsyncConcurrent(async _ =>
     {
         logger.LogInformation("{Now}: Subscription 5 starts", DateTime.Now);
         await Task.Delay(1000).ConfigureAwait(false);
         logger.LogInformation("{Now}: Subscription 5 delay 1 s", DateTime.Now);
     });
     context.StateChanges()
     .Where(n => n.Entity.EntityId == "input_select.who_cooks")
     .Subscribe(_ =>
     {
         logger.LogInformation("{Now}: Subscription 6 starts", DateTime.Now);
         Task.Delay(2000).Wait();
         logger.LogInformation("{Now}: Subscription 6 delay 2 s", DateTime.Now);
     });
 }
Esempio n. 4
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 })
         );
 }
Esempio n. 5
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);
    }