public void Initialize()
 {
     app.Entity("binary_sensor.pir_kitchen")
     .StateChanges
     .Where(e => e.New?.State == "on")
     .Subscribe(_ => app.Entity("light.somelight").TurnOn());
 }
Exemple #2
0
        /// <summary>
        ///     Initialize the app
        /// </summary>
        public void Initialize()
        {
            _app.Entity("binary_sensor.kitchen")
            .StateChanges
            .Where(e => e.New?.State == "on" && e.Old?.State == "off")
            .Subscribe(_ =>
            {
                // Just for the testing of all these
                _app.Entity("light.kitchen").TurnOn(new { brightness = 100 });
                _app.Entity("light.kitchen").TurnOff(new { brightness = 100 });
                _app.Entity("light.kitchen").Toggle(new { brightness = 100 });
            }
                       );

            _app.Entity("binary_sensor.livingroom")
            .StateChanges
            .Where(e => e.New?.State == "on" && e.Old?.State == "off")
            .Subscribe(_ => _app.Entity("sensor.mysensor").SetState(20, new { battery_level = 90 }));

            _app.Entities(n => n.EntityId !.EndsWith("entities", StringComparison.InvariantCultureIgnoreCase)).StateChanges.Where(s => s.New.State == "on").Subscribe(_ => _app.Entity("light.kitchen").TurnOn());
            _app.Entities(n => n.EntityId !.EndsWith("entities", StringComparison.InvariantCultureIgnoreCase)).StateChanges.Where(s => s.New.State == "off").Subscribe(_ => _app.Entity("light.kitchen").TurnOff());

            _app.Entity("sensor.temperature")
            .StateAllChanges
            .Where(e => e.New?.Attribute?.battery_level < 15)
            .Subscribe(_ => _app.CallService("notify", "notify", new { title = "Hello from Home Assistant" }));

            _app.EventChanges
            .Where(e => e.Event == "hello_event")
            .Subscribe(_ => _app.Entity("light.livingroom").TurnOn());

            _app.EventChanges
            .Where(e => e.Event == "bye_event")
            .Subscribe(_ => _app.Entity("light.livingroom").TurnOff());

            _app.SetState("sensor.any_sensor", 20, new { battery_level = 70 });
            _app.SetState("sensor.any_sensor2", 20, new { battery_level = 70 });

            _app.Entity("binary_sensor.test_state_entity")
            .StateChanges
            .Subscribe(_ =>
            {
                if (_app.State("sensor.some_other_entity")?.State == "on")
                {
                    _app.Entity("light.state_light").TurnOn();
                }
            });

            _app.RunIn(TimeSpan.FromMilliseconds(100), () => _app.Entity("binary_sensor.fake_run_in_happened").TurnOn());
            _app.RunEveryMinute(0, () => _app.Entity("binary_sensor.fake_run_every_minute_happened").TurnOn());
            _app.RunEveryHour("15:00", () => _app.Entity("binary_sensor.fake_run_every_hour_happened").TurnOn());
            _app.RunDaily("23:00:00", () => _app.Entity("binary_sensor.fake_run_daily_happened").TurnOn());
            _timer = _app.RunEveryMinute(0, TestTimerDisposal);
        }
 public void TurnOn()
 {
     _app.Entity(EntityId).TurnOn();
 }
        private void SetupSubscriptions()
        {
            LogTrace("SetupSubscriptions()");
            foreach (var entityId in _presenceEntityIds)
            {
                _app.Entity(entityId)
                .StateAllChanges
                .Where(e => e.Old?.State == "off" && e.New?.State == "on" || e.Old?.State == "on" && e.New?.State == "on")
                .Subscribe(s =>
                {
                    LogDebug("Presence event: {entityId}", s.New.EntityId);
                    HandleEvent();
                });
            }

            foreach (var entityId in _keepAliveEntityIds)
            {
                _app.Entity(entityId)
                .StateChanges
                .Where(e => e.Old?.State == "off" && e.New?.State == "on" || e.Old?.State == "on" && e.New?.State == "on")
                .Subscribe(s =>
                {
                    LogDebug("Keep Alive event: {entityId}", s.New.EntityId);
                    HandleEvent();
                });
            }

            foreach (var entityId in _controlEntityIds.Union(_nightControlEntityIds))
            {
                _app.Entity(entityId)
                .StateChanges
                .Where(e => e.Old?.State == "on" && e.New?.State == "off")
                .Subscribe(s =>
                {
                    LogDebug("Entitiy Manually Turned Off: {entityId}", s.New.EntityId);
                    SetRoomState(RoomState.Idle);
                });
            }

            foreach (var entityId in _controlEntityIds.Union(_nightControlEntityIds))
            {
                _app.Entity(entityId)
                .StateAllChanges
                .Where(e => e.Old?.State == "on" && e.New?.State == "on" &&
                       e.New?.Context?.UserId != null &&
                       (e.Old?.Attribute?.brightness != e.New?.Attribute?.brightness || e.Old?.Attribute?.color_temp != e.New?.Attribute?.color_temp))

                .Subscribe(s =>
                {
                    LogDebug("Brightness/Colour Manually Changed: {entityId}", s.New.EntityId);
                    DisableCircadian();
                });
            }

            foreach (var entityId in _controlEntityIds.Union(_nightControlEntityIds))
            {
                _app.Entity(entityId)
                .StateChanges
                .Where(e => e.Old?.State == "off" && e.New?.State == "on")
                .Subscribe(s =>
                {
                    LogDebug("Entitiy Manually Turned On: {entityId}", s.New.EntityId);
                    SetRoomState(RoomState.Override);
                });
            }

            _app.Entity(_roomConfig.EnabledSwitchEntityId)
            .StateChanges
            .Subscribe(s =>
            {
                LogDebug("Enabled Switch event changed to: {state}", s.New.State);
                if (s.New.State == "on")
                {
                    SetRoomState(RoomState.Idle);
                }
                if (s.New.State == "off")
                {
                    SetRoomState(RoomState.Disabled);
                }
            });

            if (_app.States.Any(s => s.EntityId == _roomConfig.NightTimeEntityId))
            {
                _app.Entity(_roomConfig.NightTimeEntityId !)
                .StateChanges
                .Subscribe(s =>
                {
                    LogDebug("Night Mode Switched: {state}", s.New.State);
                    NightModeChanged(s.New.State);
                });
            }
        }
Exemple #5
0
 public void Initialize()
 {
     _app.Entity("sensor.template_last_motion")
     .StateChanges
     .Subscribe(s => { CalcHouseMode(); });
 }