From 3b9a2cc3290a1a01becc9f929bf6d7da7c2496c2 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Thu, 7 Mar 2024 13:39:02 +0000 Subject: [PATCH 1/4] MQTT now creates Device, but sensors switched not yet appearing under it --- MainWindow.xaml.cs | 74 ++++++++++++++++++---------------------------- TEAMS2HA.csproj | 4 +-- 2 files changed, 31 insertions(+), 47 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 42d0bc7..4a3003b 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -914,83 +914,67 @@ private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e) private async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSettings settings) { + // Define common device information for all entities. + var deviceInfo = new + { + ids = new[] { "teams2ha_" + deviceid }, // Unique device identifier + mf = "Jimmy White", // Manufacturer name + mdl = "Teams2HA Device", // Model + name = deviceid, // Device name + sw = "v1.0" // Software version + }; + foreach (var sensor in sensorNames) { - var device = new Device() - { - Identifiers = deviceid, - Name = deviceid, - SwVersion = "1.0.0", - Model = "Teams2HA", - Manufacturer = "JimmyWhite", - }; - // added to check if meeting update is null - if (meetingUpdate == null) - { - meetingUpdate = new MeetingUpdate - { - MeetingState = new MeetingState - { - IsMuted = false, - IsVideoOn = false, - IsHandRaised = false, - IsInMeeting = false, - IsRecordingOn = false, - IsBackgroundBlurred = false, - IsSharing = false, - HasUnreadMessages = false - } - }; - } string sensorKey = $"{deviceid}_{sensor}"; string sensorName = $"{deviceid}_{sensor}".ToLower().Replace(" ", "_"); string deviceClass = DetermineDeviceClass(sensor); string icon = DetermineIcon(sensor, meetingUpdate.MeetingState); string stateValue = GetStateValue(sensor, meetingUpdate); + string uniqueId = $"{deviceid}_{sensor}"; + if (!_previousSensorStates.TryGetValue(sensorKey, out var previousState) || previousState != stateValue) { _previousSensorStates[sensorKey] = stateValue; // Update the stored state if (deviceClass == "switch") { - string stateTopic = $"homeassistant/switch/{sensorName}/state"; - string commandTopic = $"homeassistant/switch/{sensorName}/set"; var switchConfig = new { name = sensorName, - unique_id = sensorName, - state_topic = stateTopic, - command_topic = commandTopic, + unique_id = uniqueId, + device = deviceInfo, + icon = icon, + command_topic = $"homeassistant/switch/{sensorName}/set", + state_topic = $"homeassistant/switch/{sensorName}/state", payload_on = "ON", - payload_off = "OFF", - icon = icon + payload_off = "OFF" }; string configTopic = $"homeassistant/switch/{sensorName}/config"; - await mqttClientWrapper.PublishAsync(configTopic, JsonConvert.SerializeObject(switchConfig)); - await mqttClientWrapper.PublishAsync(stateTopic, stateValue); + await mqttClientWrapper.PublishAsync(configTopic, JsonConvert.SerializeObject(switchConfig), true); + await mqttClientWrapper.PublishAsync(switchConfig.state_topic, stateValue); } - else if (deviceClass == "sensor") // Use else-if for binary_sensor + else if (deviceClass == "sensor") { - string stateTopic = $"homeassistant/sensor/{sensorName}/state"; // Corrected state topic - var binarySensorConfig = new + var sensorConfig = new { name = sensorName, - unique_id = sensorName, - state_topic = stateTopic, - + unique_id = uniqueId, + device = deviceInfo, icon = icon, - Device = device, + state_topic = $"homeassistant/sensor/{sensorName}/state" }; string configTopic = $"homeassistant/sensor/{sensorName}/config"; - await mqttClientWrapper.PublishAsync(configTopic, JsonConvert.SerializeObject(binarySensorConfig), true); - await mqttClientWrapper.PublishAsync(stateTopic, stateValue); // Publish the state + await mqttClientWrapper.PublishAsync(configTopic, JsonConvert.SerializeObject(sensorConfig), true); + await mqttClientWrapper.PublishAsync(sensorConfig.state_topic, stateValue); } } } - } + + private async Task SaveSettingsAsync() { var settings = AppSettings.Instance; diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index d31491c..adbd1ff 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.317 - 1.1.0.317 + 1.1.0.329 + 1.1.0.329 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png From 9c0916f8466d036c43660219019078e036fc4949 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Thu, 7 Mar 2024 14:04:14 +0000 Subject: [PATCH 2/4] MQTT working as device, issues with naming --- MainWindow.xaml.cs | 20 ++++++++++++++++++-- TEAMS2HA.csproj | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 4a3003b..6dd88f3 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -923,7 +923,23 @@ private async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSetting name = deviceid, // Device name sw = "v1.0" // Software version }; - + if (meetingUpdate == null) + { + meetingUpdate = new MeetingUpdate + { + MeetingState = new MeetingState + { + IsMuted = false, + IsVideoOn = false, + IsHandRaised = false, + IsInMeeting = false, + IsRecordingOn = false, + IsBackgroundBlurred = false, + IsSharing = false, + HasUnreadMessages = false + } + }; + } foreach (var sensor in sensorNames) { string sensorKey = $"{deviceid}_{sensor}"; @@ -958,7 +974,7 @@ private async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSetting { var sensorConfig = new { - name = sensorName, + name = $"{sensorName} {deviceid}", unique_id = uniqueId, device = deviceInfo, icon = icon, diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index adbd1ff..dc83d1a 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.329 - 1.1.0.329 + 1.1.0.333 + 1.1.0.333 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png From 01a0a4b6701e739f13d904b7921ad6f1b0e6ee58 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Thu, 7 Mar 2024 14:15:53 +0000 Subject: [PATCH 3/4] MQTT --- TEAMS2HA.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index dc83d1a..a7664f9 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.333 - 1.1.0.333 + 1.1.0.337 + 1.1.0.337 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png From 141de3a6bd55050bb0ec5f38f99beefe12ac1b01 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Thu, 7 Mar 2024 14:38:24 +0000 Subject: [PATCH 4/4] MQTT Naming --- MainWindow.xaml.cs | 4 ++-- TEAMS2HA.csproj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 6dd88f3..42c37e6 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -943,7 +943,7 @@ private async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSetting foreach (var sensor in sensorNames) { string sensorKey = $"{deviceid}_{sensor}"; - string sensorName = $"{deviceid}_{sensor}".ToLower().Replace(" ", "_"); + string sensorName = $"{sensor}".ToLower().Replace(" ", "_"); string deviceClass = DetermineDeviceClass(sensor); string icon = DetermineIcon(sensor, meetingUpdate.MeetingState); string stateValue = GetStateValue(sensor, meetingUpdate); @@ -974,7 +974,7 @@ private async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSetting { var sensorConfig = new { - name = $"{sensorName} {deviceid}", + name = sensorName, unique_id = uniqueId, device = deviceInfo, icon = icon, diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index a7664f9..7997435 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.337 - 1.1.0.337 + 1.1.0.340 + 1.1.0.340 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png