Skip to content

Commit

Permalink
Merge pull request #43 from jimmyeao/dev
Browse files Browse the repository at this point in the history
Dev into master
  • Loading branch information
jimmyeao authored Mar 10, 2024
2 parents 508bf8c + 141de3a commit 12c7b6d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
90 changes: 45 additions & 45 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,83 +914,83 @@ private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)

private async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSettings settings)
{
foreach (var sensor in sensorNames)
// Define common device information for all entities.
var deviceInfo = new
{
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)
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
};
if (meetingUpdate == null)
{
meetingUpdate = new MeetingUpdate
{
meetingUpdate = new MeetingUpdate
MeetingState = new MeetingState
{
MeetingState = new MeetingState
{
IsMuted = false,
IsVideoOn = false,
IsHandRaised = false,
IsInMeeting = false,
IsRecordingOn = false,
IsBackgroundBlurred = false,
IsSharing = false,
HasUnreadMessages = false
}
};
}
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}";
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);
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;
Expand Down
4 changes: 2 additions & 2 deletions TEAMS2HA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<AssemblyVersion>1.1.0.317</AssemblyVersion>
<FileVersion>1.1.0.317</FileVersion>
<AssemblyVersion>1.1.0.340</AssemblyVersion>
<FileVersion>1.1.0.340</FileVersion>
<ApplicationIcon>Assets\Square150x150Logo.scale-200.ico</ApplicationIcon>
<Title>Teams2HA</Title>
<PackageIcon>Square150x150Logo.scale-200.png</PackageIcon>
Expand Down

0 comments on commit 12c7b6d

Please sign in to comment.