|
| 1 | +using System.Reactive; |
| 2 | +using System.Reactive.Concurrency; |
| 3 | +using System.Reactive.Linq; |
| 4 | +using CodeCasa.NetDaemon.Extensions.Observables; |
| 5 | +using CodeCasa.NetDaemon.Sensors.Composite.Generated; |
| 6 | +using NetDaemon.HassModel.Entities; |
| 7 | + |
| 8 | +namespace CodeCasa.NetDaemon.Sensors.Composite |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Provides a base implementation for a reactive motion sensor that integrates illuminance data. |
| 12 | + /// </summary> |
| 13 | + /// <remarks> |
| 14 | + /// This class handles the logic for persistent motion detection, ensuring that motion triggers |
| 15 | + /// are light-sensitive upon activation but remain active regardless of light changes until motion ceases. |
| 16 | + /// </remarks> |
| 17 | + public abstract class MotionSensor : IObservable<bool> |
| 18 | + { |
| 19 | + private readonly IScheduler _scheduler; |
| 20 | + private readonly BinarySensorEntity _binarySensorEntity; |
| 21 | + private readonly NumericSensorEntity _numericSensorEntity; |
| 22 | + |
| 23 | + private readonly IObservable<bool> _defaultObservable; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of the <see cref="MotionSensor"/> class. |
| 27 | + /// </summary> |
| 28 | + /// <param name="scheduler">The scheduler used for time-based operations.</param> |
| 29 | + /// <param name="motionOccupancySensor">The occupancy sensor entity core.</param> |
| 30 | + /// <param name="motionIlluminanceLuxSensor">The illuminance sensor entity core.</param> |
| 31 | + protected MotionSensor(IScheduler scheduler, |
| 32 | + IBinarySensorEntityCore motionOccupancySensor, |
| 33 | + ISensorEntityCore motionIlluminanceLuxSensor) |
| 34 | + { |
| 35 | + _scheduler = scheduler; |
| 36 | + _binarySensorEntity = new BinarySensorEntity(motionOccupancySensor); |
| 37 | + _numericSensorEntity = new NumericSensorEntity(motionIlluminanceLuxSensor); |
| 38 | + |
| 39 | + MotionOccupancySensor = motionOccupancySensor; |
| 40 | + MotionIlluminanceLuxSensor = motionIlluminanceLuxSensor; |
| 41 | + |
| 42 | + _defaultObservable = CreatePersistentMotionObservable(); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets the occupancy sensor entity core. |
| 47 | + /// </summary> |
| 48 | + public IBinarySensorEntityCore MotionOccupancySensor { get; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets the illuminance sensor entity core. |
| 52 | + /// </summary> |
| 53 | + public ISensorEntityCore MotionIlluminanceLuxSensor { get; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// An event stream that fires once when the motion criteria are first met (low light and movement). |
| 57 | + /// </summary> |
| 58 | + /// <remarks> |
| 59 | + /// Emits a <see cref="Unit"/> when the persistent motion state transitions from <c>false</c> to <c>true</c>. |
| 60 | + /// </remarks> |
| 61 | + public IObservable<Unit> Triggered => _defaultObservable.Where(b => b).Select(_ => Unit.Default); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// An event stream that fires once when the motion state is reset. |
| 65 | + /// </summary> |
| 66 | + /// <remarks> |
| 67 | + /// Emits a <see cref="Unit"/> when the motion sensor's <c>offDelay</c> has expired, |
| 68 | + /// signaling that occupancy is no longer detected. |
| 69 | + /// </remarks> |
| 70 | + public IObservable<Unit> Cleared => _defaultObservable.Where(b => !b).Select(_ => Unit.Default); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets an observable representing the motion state from the occupancy sensor. |
| 74 | + /// </summary> |
| 75 | + public IObservable<bool> Motion => _binarySensorEntity.ToBooleanObservable(); |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Creates an observable that tracks motion persistence based on a brightness threshold. |
| 79 | + /// </summary> |
| 80 | + /// <param name="brightnessThreshold">The maximum brightness level allowed to initially trigger the motion state.</param> |
| 81 | + /// <param name="offDelay">The duration to keep the motion state active after the sensor stops detecting movement. Defaults to 60 seconds.</param> |
| 82 | + /// <returns> |
| 83 | + /// An <see cref="IObservable{T}"/> that emits <c>true</c> when motion is detected under the brightness threshold, |
| 84 | + /// and remains <c>true</c> until the motion <paramref name="offDelay"/> expires. |
| 85 | + /// </returns> |
| 86 | + /// <remarks> |
| 87 | + /// This method implements a "latch" logic: the observable only flips to <c>true</c> if both motion is detected |
| 88 | + /// AND brightness is low. However, once triggered, it stays <c>true</c> even if brightness increases, |
| 89 | + /// until the motion sensor itself resets. |
| 90 | + /// </remarks> |
| 91 | + public IObservable<bool> CreatePersistentMotionObservable(double brightnessThreshold = 5, TimeSpan? offDelay = null) |
| 92 | + { |
| 93 | + offDelay ??= TimeSpan.FromSeconds(60); |
| 94 | + |
| 95 | + var motionLastXTime = |
| 96 | + _binarySensorEntity.PersistOnFor(offDelay.Value, _scheduler); |
| 97 | + |
| 98 | + var brightnessLessThanX = _numericSensorEntity |
| 99 | + .ToBooleanObservable(s => s.State <= brightnessThreshold); |
| 100 | + |
| 101 | + var triggered = false; |
| 102 | + return motionLastXTime.CombineLatest(brightnessLessThanX, (motionTriggered, brightnessTriggered) => |
| 103 | + { |
| 104 | + if (motionTriggered && brightnessTriggered) |
| 105 | + { |
| 106 | + triggered = true; |
| 107 | + } |
| 108 | + else if (!motionTriggered) |
| 109 | + { |
| 110 | + triggered = false; |
| 111 | + } |
| 112 | + |
| 113 | + return triggered; |
| 114 | + }).DistinctUntilChanged(); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Subscribes an observer to the default persistent motion stream. |
| 119 | + /// </summary> |
| 120 | + /// <param name="observer">The object that is to receive notifications.</param> |
| 121 | + /// <returns>A reference to an interface that allows observers to stop receiving notifications before the provider has finished sending them.</returns> |
| 122 | + public IDisposable Subscribe(IObserver<bool> observer) => _defaultObservable.Subscribe(observer); |
| 123 | + } |
| 124 | +} |
0 commit comments