Skip to content

Commit

Permalink
1.1.14 - Improve MQTT unit, output and flag capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
rwagoner committed Nov 2, 2022
1 parent a016e1c commit 495ce74
Show file tree
Hide file tree
Showing 41 changed files with 386 additions and 148 deletions.
36 changes: 1 addition & 35 deletions OmniLinkBridge/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OmniLinkBridge.MQTT;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand All @@ -24,39 +23,6 @@ public static bool IsBitSet(this byte b, int pos)
{
return (b & (1 << pos)) != 0;
}

public static AreaCommandCode ToCommandCode(this string payload, bool supportValidate = false)
{
string[] payloads = payload.Split(',');
int code = 0;

AreaCommandCode ret = new AreaCommandCode()
{
Command = payloads[0]
};

if (payload.Length == 1)
return ret;

if (payloads.Length == 2)
{
ret.Success = int.TryParse(payloads[1], out code);
}
else if (supportValidate && payloads.Length == 3)
{
if (string.Compare(payloads[1], "validate", true) == 0)
{
ret.Validate = true;
ret.Success = int.TryParse(payloads[2], out code);
}
else
ret.Success = false;
}

ret.Code = code;
return ret;
}

public static string ToSpaceTitleCase(this string phrase)
{
return Regex.Replace(phrase, "(\\B[A-Z])", " $1");
Expand Down
1 change: 1 addition & 0 deletions OmniLinkBridge/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public abstract class Global
public static HashSet<int> mqtt_discovery_ignore_units;
public static HashSet<int> mqtt_discovery_area_code_required;
public static ConcurrentDictionary<int, MQTT.OverrideZone> mqtt_discovery_override_zone;
public static ConcurrentDictionary<int, MQTT.OverrideUnit> mqtt_discovery_override_unit;

// Notifications
public static bool notify_area;
Expand Down
65 changes: 65 additions & 0 deletions OmniLinkBridge/MQTT/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using HAI_Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OmniLinkBridge.MQTT
{
public static class Extensions
{
public static AreaCommandCode ToCommandCode(this string payload, bool supportValidate = false)
{
string[] payloads = payload.Split(',');
int code = 0;

AreaCommandCode ret = new AreaCommandCode()
{
Command = payloads[0]
};

if (payload.Length == 1)
return ret;

if (payloads.Length == 2)
{
ret.Success = int.TryParse(payloads[1], out code);
}
else if (supportValidate && payloads.Length == 3)
{
if (string.Compare(payloads[1], "validate", true) == 0)
{
ret.Validate = true;
ret.Success = int.TryParse(payloads[2], out code);
}
else
ret.Success = false;
}

ret.Code = code;
return ret;
}

public static UnitType ToUnitType(this clsUnit unit)
{
Global.mqtt_discovery_override_unit.TryGetValue(unit.Number, out OverrideUnit override_unit);

if (unit.Type == enuOL2UnitType.Output)
return UnitType.@switch;

if (unit.Type == enuOL2UnitType.Flag)
{
if (override_unit != null && override_unit.type == UnitType.number)
return UnitType.number;

return UnitType.@switch;
}

if (override_unit != null && override_unit.type == UnitType.@switch)
return UnitType.@switch;

return UnitType.light;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class Alarm : Device
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class BinarySensor : Device
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class Climate : Device
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using OmniLinkBridge.Modules;
using System.Collections.Generic;

namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class Device
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class DeviceRegistry
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class Light : Device
{
Expand Down
18 changes: 18 additions & 0 deletions OmniLinkBridge/MQTT/HomeAssistant/Number.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Newtonsoft.Json;

namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class Number : Device
{
public string command_topic { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string icon { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int? min { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int? max { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class Sensor : Device
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.HomeAssistant
{
public class Switch : Device
{
Expand Down
16 changes: 16 additions & 0 deletions OmniLinkBridge/MQTT/MappingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using HAI_Shared;
using Newtonsoft.Json;
using System.Collections.Generic;
using OmniLinkBridge.MQTT.HomeAssistant;
using OmniLinkBridge.MQTT.Parser;

namespace OmniLinkBridge.MQTT
{
Expand Down Expand Up @@ -399,6 +401,20 @@ public static Switch ToConfigSwitch(this clsUnit unit)
return ret;
}

public static Number ToConfigNumber(this clsUnit unit)
{
Number ret = new Number
{
unique_id = $"{Global.mqtt_prefix}unit{unit.Number}number",
name = Global.mqtt_discovery_name_prefix + unit.Name,
state_topic = unit.ToTopic(Topic.flag_state),
command_topic = unit.ToTopic(Topic.flag_command),
min = 0,
max = 255
};
return ret;
}

public static string ToState(this clsUnit unit)
{
return unit.Status == 0 || unit.Status == 100 ? UnitCommands.OFF.ToString() : UnitCommands.ON.ToString();
Expand Down
15 changes: 11 additions & 4 deletions OmniLinkBridge/MQTT/MessageProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HAI_Shared;
using OmniLinkBridge.MQTT.Parser;
using OmniLinkBridge.OmniLink;
using Serilog;
using System;
Expand Down Expand Up @@ -167,21 +168,27 @@ private void ProcessUnitReceived(clsUnit unit, Topic command, string payload)
OmniLink.SendCommand(UnitMapping[cmd], 0, (ushort)unit.Number);
}
}
else if (command == Topic.brightness_command && int.TryParse(payload, out int unitValue))
else if (unit.Type == enuOL2UnitType.Flag &&
command == Topic.flag_command && int.TryParse(payload, out int flagValue))
{
log.Debug("SetUnit: {id} to {value}", unit.Number, payload);
OmniLink.SendCommand(enuUnitCommand.Set, BitConverter.GetBytes(flagValue)[0], (ushort)unit.Number);
}
else if (unit.Type != enuOL2UnitType.Output &&
command == Topic.brightness_command && int.TryParse(payload, out int unitValue))
{
log.Debug("SetUnit: {id} to {value}%", unit.Number, payload);

OmniLink.SendCommand(enuUnitCommand.Level, BitConverter.GetBytes(unitValue)[0], (ushort)unit.Number);

// Force status change instead of waiting on controller to update
// Home Assistant sends brightness immediately followed by ON,
// which will cause light to go to 100% brightness
unit.Status = (byte)(100 + unitValue);
}
else if (command == Topic.scene_command && char.TryParse(payload, out char scene))
else if (unit.Type != enuOL2UnitType.Output &&
command == Topic.scene_command && char.TryParse(payload, out char scene))
{
log.Debug("SetUnit: {id} to {value}", unit.Number, payload);

OmniLink.SendCommand(enuUnitCommand.Compose, (byte)(scene - 63), (ushort)unit.Number);
}
}
Expand Down
12 changes: 0 additions & 12 deletions OmniLinkBridge/MQTT/Number.cs

This file was deleted.

7 changes: 7 additions & 0 deletions OmniLinkBridge/MQTT/OverrideUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OmniLinkBridge.MQTT
{
public class OverrideUnit
{
public UnitType type { get; set; }
}
}
4 changes: 3 additions & 1 deletion OmniLinkBridge/MQTT/OverrideZone.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OmniLinkBridge.MQTT
using OmniLinkBridge.MQTT.HomeAssistant;

namespace OmniLinkBridge.MQTT
{
public class OverrideZone
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.Parser
{
enum AlarmCommands
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.Parser
{
enum AreaCommands
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.Parser
{
enum CommandTypes
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.Parser
{
enum MessageCommands
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.Parser
{
public enum Topic
{
Expand All @@ -11,6 +11,8 @@ public enum Topic
json_state,
brightness_state,
brightness_command,
flag_state,
flag_command,
scene_state,
scene_command,
current_operation,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.Parser
{
enum UnitCommands
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OmniLinkBridge.MQTT
namespace OmniLinkBridge.MQTT.Parser
{
enum ZoneCommands
{
Expand Down
9 changes: 9 additions & 0 deletions OmniLinkBridge/MQTT/UnitType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OmniLinkBridge.MQTT
{
public enum UnitType
{
@switch,
light,
number
}
}
Loading

0 comments on commit 495ce74

Please sign in to comment.