Skip to content

Commit

Permalink
added home assistant availability_topic logic (to show sensor unavail…
Browse files Browse the repository at this point in the history
…able if application is not running)
  • Loading branch information
mhwlng committed Feb 18, 2023
1 parent 3c3d82a commit 1885842
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
5 changes: 4 additions & 1 deletion fipha/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ protected override void OnStartup(StartupEventArgs evtArgs)
if (onlyOnce)
{
onlyOnce = false;
await MQTT.Publish($"homeassistant/death", "online");
Log.Info($"HWINFO Sensors found, Writing all HWINFO Sensors to hwinfo.json");
Expand All @@ -431,7 +433,8 @@ protected override void OnStartup(StartupEventArgs evtArgs)
unit_of_measurement = element.Value.Unit,
value_template = "{{ value_json.value}}",
unique_id = element.Value.Node,
state_class = "measurement"
state_class = "measurement",
availability_topic = "homeassistant/death"
}, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
Expand Down
28 changes: 22 additions & 6 deletions fipha/HWInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public class MQTTDiscoveryObj
public string value_template;
public string unique_id;
public string state_class;
public string availability_topic;
}

public class MQTTStateObj
Expand Down Expand Up @@ -409,14 +410,19 @@ private static void ReadSensors(MemoryMappedFile mmf, _HWiNFO_SHARED_MEM hWiNFOM

if (!FullSensorData.ContainsKey(index))
{
var sensorNameOrig = Encoding.GetEncoding(1252).GetString(structure.SensorNameOrig).TrimEnd((char)0);
//var sensorNameOrig = Encoding.GetEncoding(1252).GetString(structure.SensorNameOrig).TrimEnd((char)0);
var sensorNameOrig = Encoding.UTF8.GetString(structure.SensorNameOrig).TrimEnd((char)0);

var sensorName = Encoding.GetEncoding(1252).GetString(structure.SensorNameUser).TrimEnd((char)0);
var sensorName = "";

if (hWiNFOMemory.Version > 1)
{
sensorName = Encoding.UTF8.GetString(structure.UtfSensorNameUser).TrimEnd((char)0);

}
else
{
//sensorName = Encoding.GetEncoding(1252).GetString(structure.SensorNameUser).TrimEnd((char)0);
sensorName = Encoding.UTF8.GetString(structure.SensorNameUser).TrimEnd((char)0);
}

var sensor = new SensorObj
Expand Down Expand Up @@ -465,21 +471,31 @@ private static void ReadElements(MemoryMappedFile mmf, _HWiNFO_SHARED_MEM hWiNFO

var elementKey = sensor.SensorId + "-" + sensor.SensorInstance + "-" + structure.ElementId;

var labelOrig = Encoding.GetEncoding(1252).GetString(structure.LabelOrig).TrimEnd((char)0);
//var labelOrig = Encoding.GetEncoding(1252).GetString(structure.LabelOrig).TrimEnd((char)0);
var labelOrig = Encoding.UTF8.GetString(structure.LabelOrig).TrimEnd((char)0);

var unit = Encoding.GetEncoding(1252).GetString(structure.Unit).TrimEnd((char)0);
var unit = "";

if (hWiNFOMemory.Version > 1)
{
unit = Encoding.UTF8.GetString(structure.UtfUnit).TrimEnd((char)0);
} else
{
//unit = Encoding.GetEncoding(1252).GetString(structure.Unit).TrimEnd((char)0);
unit = Encoding.UTF8.GetString(structure.Unit).TrimEnd((char)0);
}

var label = System.Text.Encoding.GetEncoding(1252).GetString(structure.LabelUser).TrimEnd((char)0);
var label = "?";

if (hWiNFOMemory.Version > 1)
{
label = Encoding.UTF8.GetString(structure.UtfLabelUser).TrimEnd((char)0);
}
else
{
//label = System.Text.Encoding.GetEncoding(1252).GetString(structure.LabelUser).TrimEnd((char)0);
label = System.Text.Encoding.UTF8.GetString(structure.LabelUser).TrimEnd((char)0);
}

var element = new ElementObj
{
Expand Down
5 changes: 5 additions & 0 deletions fipha/Mqtt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public static async Task<bool> Connect()
.WithClientId(ClientId)
.WithCredentials(_mqttUser, _mqttPassword)
.WithTcpServer(_mqttUri, _mqttPort)

.WithWillTopic("homeassistant/death")
.WithWillPayload("offline")
.WithWillQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce)
.WithWillRetain(true)
.WithCleanSession();

var options = _mqttSecure
Expand Down
6 changes: 3 additions & 3 deletions fipha/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("fipha")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -31,7 +31,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.1.4")]
[assembly: AssemblyFileVersion("0.0.1.4")]
[assembly: AssemblyVersion("0.0.1.5")]
[assembly: AssemblyFileVersion("0.0.1.5")]

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

0 comments on commit 1885842

Please sign in to comment.