-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinarySensorCommandClass.SupportedSensor.cs
More file actions
84 lines (66 loc) · 3.06 KB
/
BinarySensorCommandClass.SupportedSensor.cs
File metadata and controls
84 lines (66 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Microsoft.Extensions.Logging;
namespace ZWave.CommandClasses;
public sealed partial class BinarySensorCommandClass
{
/// <summary>
/// Gets the supported sensor types reported by the device.
/// </summary>
public IReadOnlySet<BinarySensorType>? SupportedSensorTypes { get; private set; }
/// <summary>
/// Request the supported sensor types from the binary sensor device.
/// </summary>
public async Task<IReadOnlySet<BinarySensorType>> GetSupportedSensorTypesAsync(CancellationToken cancellationToken)
{
BinarySensorSupportedGetCommand command = BinarySensorSupportedGetCommand.Create();
await SendCommandAsync(command, cancellationToken).ConfigureAwait(false);
CommandClassFrame reportFrame = await AwaitNextReportAsync<BinarySensorSupportedReportCommand>(cancellationToken).ConfigureAwait(false);
IReadOnlySet<BinarySensorType> supportedTypes = BinarySensorSupportedReportCommand.Parse(reportFrame, Logger);
SupportedSensorTypes = supportedTypes;
Dictionary<BinarySensorType, bool?> newSensorValues = [];
foreach (BinarySensorType type in supportedTypes)
{
// Persist any existing known state.
if (!_sensorValues.TryGetValue(type, out bool? existing))
{
existing = null;
}
newSensorValues.Add(type, existing);
}
_sensorValues = newSensorValues;
return supportedTypes;
}
internal readonly struct BinarySensorSupportedGetCommand : ICommand
{
public BinarySensorSupportedGetCommand(CommandClassFrame frame)
{
Frame = frame;
}
public static CommandClassId CommandClassId => CommandClassId.BinarySensor;
public static byte CommandId => (byte)BinarySensorCommand.SupportedGet;
public CommandClassFrame Frame { get; }
public static BinarySensorSupportedGetCommand Create()
{
CommandClassFrame frame = CommandClassFrame.Create(CommandClassId, CommandId);
return new BinarySensorSupportedGetCommand(frame);
}
}
internal readonly struct BinarySensorSupportedReportCommand : ICommand
{
public BinarySensorSupportedReportCommand(CommandClassFrame frame)
{
Frame = frame;
}
public static CommandClassId CommandClassId => CommandClassId.BinarySensor;
public static byte CommandId => (byte)BinarySensorCommand.SupportedReport;
public CommandClassFrame Frame { get; }
public static IReadOnlySet<BinarySensorType> Parse(CommandClassFrame frame, ILogger logger)
{
if (frame.CommandParameters.Length < 1)
{
logger.LogWarning("Binary Sensor Supported Report frame is too short ({Length} bytes)", frame.CommandParameters.Length);
ZWaveException.Throw(ZWaveErrorCode.InvalidPayload, "Binary Sensor Supported Report frame is too short");
}
return BitMaskHelper.ParseBitMask<BinarySensorType>(frame.CommandParameters.Span);
}
}
}