Beispiel #1
0
    /// <summary>
    ///     Initialize, is automatically run by the daemon
    /// </summary>
    public override void Initialize()
    {
        // Set up the state management

        // When chromecast is going off
        // Entities("media_player.tv_nere")
        //     .StateChanges
        //     .Where(e => e.New?.State == "off" && e.Old?.State is object )
        //     .Subscribe(s =>
        //    {
        //        if (TvIsOn)
        //        {
        //            Log('TV is off, turnin off shield and tv')
        //            RunScript("tv_off_scene");
        //        }
        //    });
        // When state change on my media players, call OnMediaStateChanged
        Entities(TvMediaPlayers !)
        .StateChanges
        .Subscribe(s =>
        {
            OnMediaStateChanged(s.New, s.Old);
        });

        // When TV on (remote on), call OnTvTurnedOn
        Entity(RemoteTVRummet !)
        .StateChanges
        .Where(e =>
               e.Old?.State == "off" &&
               e.New?.State == "on")
        .Subscribe(s =>
        {
            LogDebug("TV remote status change from {from} to {to}", s.Old?.State, s.New?.State);
            OnTVTurnedOn();
        });


        // When ever TV remote activity changes, ie TV, Film, Poweroff call OnTvActivityChange
        Entity(RemoteTVRummet !)
        .StateAllChanges
        .Where(e => e.New?.Attribute?.current_activity != e.Old?.Attribute?.current_activity)
        .Subscribe(s =>
        {
            LogDebug("TV remote activity change from {from} to {to}", s.Old?.Attribute?.current_activity, s.New?.Attribute?.current_activity);
            OnTvActivityChange(s.New);
        });
    }
Beispiel #2
0
    /// <summary>
    ///     Initialize, is automatically run by the daemon
    /// </summary>
    public override void Initialize()
    {
        // Set up the state management

        // When chromecast is going off
        // Entities("media_player.tv_nere")
        //     .StateChanges
        //     .Where(e => e.New?.State == "off" && e.Old?.State is object )
        //     .Subscribe(s =>
        //    {
        //        if (TvIsOn)
        //        {
        //            Log('TV is off, turnin off shield and tv')
        //            RunScript("tv_off_scene");
        //        }
        //    });
        // When state change on my media players, call OnMediaStateChanged
        Entities(TvMediaPlayers !)
        .StateChanges
        .Subscribe(s => OnMediaStateChanged(s.New, s.Old));

        // When TV on (remote on), call OnTvTurnedOn
        Entity(RemoteTVRummet !)
        .StateChanges
        .Where(e =>
               e.Old?.State == "off" &&
               e.New?.State == "on")
        .Subscribe(s => OnTVTurnedOn());


        // When ever TV remote activity changes, ie TV, Film, Poweroff call OnTvActivityChange
        Entity(RemoteTVRummet !)
        .StateAllChanges
        .Where(e => e.New?.Attribute?.current_activity != e.Old?.Attribute?.current_activity)
        .Subscribe(s => OnTvActivityChange(s.New));

        // This function does not contain any async calls so just return completed task
        // return Task.CompletedTask;
    }
Beispiel #3
0
    /// <summary>
    ///     Initialize, is automatically run by the daemon
    /// </summary>
    public override void Initialize()
    {
        // Set up the state management

        // When state change on my media players, call OnMediaStateChanged
        Entities(TvMediaPlayers !)
        .StateChanges
        .Subscribe(s => OnMediaStateChanged(s.New, s.Old));

        // When TV on (remote on), call OnTvTurnedOn
        Entity(RemoteTVRummet !)
        .StateChanges
        .Where(e => e.New?.State == "on")
        .Subscribe(s => OnTVTurnedOn());

        // When ever TV remote activity changes, ie TV, Film, Poweroff call OnTvActivityChange
        Entity(RemoteTVRummet !)
        .StateAllChanges
        .Where(e => e.New?.Attribute?.current_activity != e.Old?.Attribute?.current_activity)
        .Subscribe(s => OnTvActivityChange(s.New));

        // This function does not contain any async calls so just return completed task
        // return Task.CompletedTask;
    }
        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);
                });
            }
        }