Skip to content

Commit

Permalink
Merge pull request #60 from zivillian/ha_boolean
Browse files Browse the repository at this point in the history
fix output of BinaryReadOnlyConverterTemplate and corresponding ha discovery topics
  • Loading branch information
zivillian authored May 24, 2023
2 parents 2dfc29f + 0b35322 commit 8abbca4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 47 deletions.
54 changes: 21 additions & 33 deletions src/ism7mqtt/HomeAssistant/HaDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ private string GetDiscoveryTopicSuffix(ParameterDescriptor descriptor)
{
if (descriptor is ListParameterDescriptor list)
{
if (!list.IsWritable && list.IsBoolean)
{
return String.Empty;
}
return "/text";
}
else
Expand Down Expand Up @@ -193,42 +189,34 @@ private string GetHomeAssistantType(ParameterDescriptor descriptor)
}
break;
case ListParameterDescriptor list:
if (list.IsWritable)
if (list.IsBoolean)
{
if (list.IsBoolean)
if (list.Options.Any(x => x.Value == "Ein"))
{
if (list.Options.Any(x => x.Value == "Ein"))
{
yield return("payload_on", "Ein");
}
if (list.Options.Any(x => x.Value == "Aktiviert"))
{
yield return("payload_on", "Aktiviert");
}
if (list.Options.Any(x => x.Value == "Aus"))
{
yield return("payload_off", "Aus");
}
if (list.Options.Any(x => x.Value == "Deaktiviert"))
{
yield return("payload_off", "Deaktiviert");
}
yield return ("payload_on", "Ein");
}
else
if (list.Options.Any(x => x.Value == "Aktiviert"))
{
var options = new JsonArray();
foreach (var value in list.Options)
{
options.Add(value.Value);
}
yield return("options", options);

yield return ("payload_on", "Aktiviert");
}
if (list.Options.Any(x => x.Value == "Aus"))
{
yield return ("payload_off", "Aus");
}
if (list.Options.Any(x => x.Value == "Deaktiviert"))
{
yield return ("payload_off", "Deaktiviert");
}
}
else if (list.IsBoolean)
else
{
yield return("payload_on", "true");
yield return("payload_off", "false");
var options = new JsonArray();
foreach (var value in list.Options)
{
options.Add(value.Value);
}
yield return ("options", options);

}
break;
default:
Expand Down
10 changes: 0 additions & 10 deletions src/ism7mqtt/ISM7/Ism7Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,16 +475,6 @@ private IEnumerable<MqttMessage> HandleListParameter(MqttMessage value)
if (listDescriptor.Options.Any())
{
var key = value.Content;
// some list types have a binary/bool converter, even if it doesn't make much sense.. try to detect those cases
if (!listDescriptor.IsBoolean && _converter is BinaryReadOnlyConverterTemplate)
{
key = key switch
{
"false" => "0",
"true" => "1",
_ => key
};
}
var text = listDescriptor.Options.Where(x => x.Key == key).Select(x => x.Value).FirstOrDefault();
if (!String.IsNullOrEmpty(text))
{
Expand Down
4 changes: 3 additions & 1 deletion src/ism7mqtt/ISM7/Xml/BinaryReadOnlyConverterTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
using System.Xml.Serialization;
using ism7mqtt.ISM7.Protocol;
Expand All @@ -22,13 +23,14 @@ protected override void AddTelegram(byte low, byte high)
_bit = (value >> Bitnumber) & 0x1;
}

[MemberNotNullWhen(true, nameof(_bit))]
public override bool HasValue => _bit.HasValue;

public override JsonValue GetValue()
{
if (!HasValue)
throw new InvalidOperationException();
var result = JsonValue.Create(_bit.Value == OnValue);
var result = JsonValue.Create(_bit.Value == OnValue ? 1 : 0);
_bit = null;
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ism7mqtt/ISM7/Xml/MixerStateConverterTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public override JsonValue GetValue()
JsonValue result;
if (_open.HasValue && _open.Value != 0)
{
result = JsonValue.Create("opened");
result = JsonValue.Create(2);
}
else if (_close.HasValue && _close.Value != 0)
{
result = JsonValue.Create("closed");
result = JsonValue.Create(1);
}
else
{
result = JsonValue.Create("-");
result = JsonValue.Create(0);
}
_open = null;
_close = null;
Expand Down

0 comments on commit 8abbca4

Please sign in to comment.