Skip to content

Commit ab4d1df

Browse files
committed
Updates to a later version of the underlying Ecobee library that should avoid token refresh race condition
1 parent e22ccfd commit ab4d1df

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

src/HomeAutio.Mqtt.Ecobee/EcobeeMqttService.cs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -355,32 +355,32 @@ private async Task UpdateState(Thermostat thermostat)
355355
thermostatStatus.Status["hvacMode"] = thermostat.Settings.HvacMode;
356356
thermostatStatus.Status["humidifierMode"] = thermostat.Settings.HumidifierMode;
357357
thermostatStatus.Status["dehumidifierMode"] = thermostat.Settings.DehumidifierMode;
358-
thermostatStatus.Status["autoAway"] = thermostat.Settings.AutoAway ? "true" : "false";
358+
thermostatStatus.Status["autoAway"] = thermostat.Settings.AutoAway.HasValue && thermostat.Settings.AutoAway.Value ? "true" : "false";
359359
thermostatStatus.Status["vent"] = thermostat.Settings.Vent;
360-
thermostatStatus.Status["actualTemperature"] = (thermostat.Runtime.ActualTemperature / 10m).ToString();
361-
thermostatStatus.Status["actualHumidity"] = thermostat.Runtime.ActualHumidity.ToString();
362-
thermostatStatus.Status["desiredHeat"] = (thermostat.Runtime.DesiredHeat / 10m).ToString();
363-
thermostatStatus.Status["desiredCool"] = (thermostat.Runtime.DesiredCool / 10m).ToString();
364-
thermostatStatus.Status["desiredHumidity"] = thermostat.Runtime.DesiredHumidity.ToString();
365-
thermostatStatus.Status["desiredDehumidity"] = thermostat.Runtime.DesiredDehumidity.ToString();
360+
thermostatStatus.Status["actualTemperature"] = thermostat.Runtime.ActualTemperature.HasValue ? (thermostat.Runtime.ActualTemperature.Value / 10m).ToString() : null;
361+
thermostatStatus.Status["actualHumidity"] = thermostat.Runtime.ActualHumidity.HasValue ? thermostat.Runtime.ActualHumidity.Value.ToString() : null;
362+
thermostatStatus.Status["desiredHeat"] = thermostat.Runtime.DesiredHeat.HasValue ? (thermostat.Runtime.DesiredHeat.Value / 10m).ToString() : null;
363+
thermostatStatus.Status["desiredCool"] = thermostat.Runtime.DesiredCool.HasValue ? (thermostat.Runtime.DesiredCool.Value / 10m).ToString() : null;
364+
thermostatStatus.Status["desiredHumidity"] = thermostat.Runtime.DesiredHumidity.HasValue ? thermostat.Runtime.DesiredHumidity.Value.ToString() : null;
365+
thermostatStatus.Status["desiredDehumidity"] = thermostat.Runtime.DesiredDehumidity.HasValue ? thermostat.Runtime.DesiredDehumidity.Value.ToString() : null;
366366
thermostatStatus.Status["desiredFanMode"] = thermostat.Runtime.DesiredFanMode;
367367

368368
// Weather forcasts
369369
var forecast = thermostat.Weather.Forecasts?.FirstOrDefault();
370370
if (forecast != null)
371371
{
372-
thermostatStatus.Status["weatherDewPoint"] = (forecast.Dewpoint / 10m).ToString();
373-
thermostatStatus.Status["weatherPrecipitationChance"] = forecast.Pop.ToString();
374-
thermostatStatus.Status["weatherPressure"] = (forecast.Pressure / 100m).ToString();
375-
thermostatStatus.Status["weatherRelativeHumidity"] = forecast.RelativeHumidity.ToString();
376-
thermostatStatus.Status["weatherTemperature"] = (forecast.Temperature / 10m).ToString();
377-
thermostatStatus.Status["weatherTempLow"] = (forecast.TempLow / 10m).ToString();
378-
thermostatStatus.Status["weatherTempHigh"] = (forecast.TempHigh / 10m).ToString();
379-
thermostatStatus.Status["weatherVisibility"] = forecast.Visibility.ToString();
380-
thermostatStatus.Status["weatherWindBearing"] = forecast.WindBearing.ToString();
381-
thermostatStatus.Status["weatherWindDirection"] = forecast.WindDirection.ToString();
382-
thermostatStatus.Status["weatherWindGust"] = forecast.WindGust.ToString();
383-
thermostatStatus.Status["weatherWindSpeed"] = forecast.WindSpeed.ToString();
372+
thermostatStatus.Status["weatherDewPoint"] = forecast.Dewpoint.HasValue ? (forecast.Dewpoint.Value / 10m).ToString() : null;
373+
thermostatStatus.Status["weatherPrecipitationChance"] = forecast.Pop.HasValue ? forecast.Pop.Value.ToString() : null;
374+
thermostatStatus.Status["weatherPressure"] = forecast.Pressure.HasValue ? (forecast.Pressure.Value / 100m).ToString() : null;
375+
thermostatStatus.Status["weatherRelativeHumidity"] = forecast.RelativeHumidity.HasValue ? forecast.RelativeHumidity.Value.ToString() : null;
376+
thermostatStatus.Status["weatherTemperature"] = forecast.Temperature.HasValue ? (forecast.Temperature.Value / 10m).ToString() : null;
377+
thermostatStatus.Status["weatherTempLow"] = forecast.TempLow.HasValue ? (forecast.TempLow.Value / 10m).ToString() : null;
378+
thermostatStatus.Status["weatherTempHigh"] = forecast.TempHigh.HasValue ? (forecast.TempHigh.Value / 10m).ToString() : null;
379+
thermostatStatus.Status["weatherVisibility"] = forecast.Visibility.HasValue ? forecast.Visibility.Value.ToString() : null;
380+
thermostatStatus.Status["weatherWindBearing"] = forecast.WindBearing.HasValue ? forecast.WindBearing.Value.ToString() : null;
381+
thermostatStatus.Status["weatherWindDirection"] = forecast.WindDirection;
382+
thermostatStatus.Status["weatherWindGust"] = forecast.WindGust.HasValue ? forecast.WindGust.Value.ToString() : null;
383+
thermostatStatus.Status["weatherWindSpeed"] = forecast.WindSpeed.HasValue ? forecast.WindSpeed.Value.ToString() : null;
384384
}
385385

386386
// Sensors
@@ -408,29 +408,30 @@ private async Task UpdateState(Thermostat thermostat)
408408

409409
// Hold
410410
var holdEvent = thermostat.Events.FirstOrDefault(x => x.Type == "hold");
411-
if (holdEvent != null && holdEvent.Running)
411+
if (holdEvent != null && holdEvent.Running.HasValue && holdEvent.Running.Value)
412412
{
413-
thermostatStatus.ActiveHold["running"] = holdEvent.Running.ToString();
414-
thermostatStatus.ActiveHold["startTime"] = DateTime.Parse($"{holdEvent.StartDate} {holdEvent.StartTime}").ToString();
415-
thermostatStatus.ActiveHold["endTime"] = DateTime.Parse($"{holdEvent.EndDate} {holdEvent.EndTime}").ToString();
416-
thermostatStatus.ActiveHold["coldHoldTemp"] = (holdEvent.CoolHoldTemp / 10m).ToString();
417-
thermostatStatus.ActiveHold["heatHoldTemp"] = (holdEvent.HeatHoldTemp / 10m).ToString();
413+
thermostatStatus.ActiveHold["running"] = holdEvent.Running.HasValue && holdEvent.Running.Value ? "true" : "false";
414+
thermostatStatus.ActiveHold["startTime"] = DateTime.TryParse($"{holdEvent.StartDate} {holdEvent.StartTime}", out var startTimeResult) ? startTimeResult.ToString() : null;
415+
thermostatStatus.ActiveHold["endTime"] = DateTime.TryParse($"{holdEvent.EndDate} {holdEvent.EndTime}", out var endTimeResult) ? endTimeResult.ToString() : null;
416+
thermostatStatus.ActiveHold["coldHoldTemp"] = holdEvent.CoolHoldTemp.HasValue ? (holdEvent.CoolHoldTemp.Value / 10m).ToString() : null;
417+
thermostatStatus.ActiveHold["heatHoldTemp"] = holdEvent.HeatHoldTemp.HasValue ? (holdEvent.HeatHoldTemp.Value / 10m).ToString() : null;
418418
thermostatStatus.ActiveHold["fan"] = holdEvent.Fan;
419-
thermostatStatus.ActiveHold["fanMinOnTime"] = holdEvent.FanMinOnTime.ToString();
419+
thermostatStatus.ActiveHold["fanMinOnTime"] = holdEvent.FanMinOnTime.HasValue ? holdEvent.FanMinOnTime.Value.ToString() : null;
420420
thermostatStatus.ActiveHold["vent"] = holdEvent.Vent;
421-
thermostatStatus.ActiveHold["ventilatorMinOnTime"] = holdEvent.VentilatorMinOnTime.ToString();
421+
thermostatStatus.ActiveHold["ventilatorMinOnTime"] = holdEvent.VentilatorMinOnTime.HasValue ? holdEvent.VentilatorMinOnTime.Value.ToString() : null;
422422
}
423423

424424
if (_thermostatStatus.ContainsKey(thermostat.Identifier))
425425
{
426426
// Publish updates
427427
foreach (var device in thermostatStatus.EquipmentStatus)
428428
{
429-
if (device.Value != _thermostatStatus[thermostat.Identifier].EquipmentStatus[device.Key])
429+
if (device.Value != _thermostatStatus[thermostat.Identifier].EquipmentStatus[device.Key] &&
430+
device.Value != null)
430431
{
431432
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
432433
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/{device.Key}")
433-
.WithPayload(device.Value.ToString())
434+
.WithPayload(device.Value)
434435
.WithAtLeastOnceQoS()
435436
.WithRetainFlag()
436437
.Build())
@@ -440,7 +441,8 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
440441

441442
foreach (var status in thermostatStatus.Status)
442443
{
443-
if (status.Value != _thermostatStatus[thermostat.Identifier].Status[status.Key])
444+
if (status.Value != _thermostatStatus[thermostat.Identifier].Status[status.Key] &&
445+
status.Value != null)
444446
{
445447
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
446448
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/{status.Key}")
@@ -455,7 +457,8 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
455457
// Hold status
456458
foreach (var holdStatus in thermostatStatus.ActiveHold)
457459
{
458-
if (holdStatus.Value != _thermostatStatus[thermostat.Identifier].ActiveHold[holdStatus.Key])
460+
if (holdStatus.Value != _thermostatStatus[thermostat.Identifier].ActiveHold[holdStatus.Key] &&
461+
holdStatus.Value != null)
459462
{
460463
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
461464
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/hold/{holdStatus.Key}")
@@ -472,7 +475,8 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
472475
{
473476
foreach (var sensorCapability in sensor.Value)
474477
{
475-
if (!_thermostatStatus[thermostat.Identifier].Sensors.ContainsKey(sensor.Key))
478+
if (!_thermostatStatus[thermostat.Identifier].Sensors.ContainsKey(sensor.Key) &&
479+
sensorCapability.Value != null)
476480
{
477481
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
478482
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/sensor/{sensor.Key.Sluggify()}/{sensorCapability.Key}")
@@ -482,7 +486,8 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
482486
.Build())
483487
.ConfigureAwait(false);
484488
}
485-
else if (!_thermostatStatus[thermostat.Identifier].Sensors[sensor.Key].ContainsKey(sensorCapability.Key))
489+
else if (!_thermostatStatus[thermostat.Identifier].Sensors[sensor.Key].ContainsKey(sensorCapability.Key) &&
490+
sensorCapability.Value != null)
486491
{
487492
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
488493
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/sensor/{sensor.Key.Sluggify()}/{sensorCapability.Key}")
@@ -492,7 +497,8 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
492497
.Build())
493498
.ConfigureAwait(false);
494499
}
495-
else if (sensorCapability.Value != _thermostatStatus[thermostat.Identifier].Sensors[sensor.Key][sensorCapability.Key])
500+
else if (sensorCapability.Value != _thermostatStatus[thermostat.Identifier].Sensors[sensor.Key][sensorCapability.Key] &&
501+
sensorCapability.Value != null)
496502
{
497503
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
498504
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/sensor/{sensor.Key.Sluggify()}/{sensorCapability.Key}")
@@ -508,18 +514,18 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
508514
else
509515
{
510516
// Publish initial state
511-
foreach (var device in thermostatStatus.EquipmentStatus)
517+
foreach (var device in thermostatStatus.EquipmentStatus.Where(x => x.Value != null))
512518
{
513519
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
514520
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/{device.Key}")
515-
.WithPayload(device.Value.ToString())
521+
.WithPayload(device.Value)
516522
.WithAtLeastOnceQoS()
517523
.WithRetainFlag()
518524
.Build())
519525
.ConfigureAwait(false);
520526
}
521527

522-
foreach (var status in thermostatStatus.Status)
528+
foreach (var status in thermostatStatus.Status.Where(x => x.Value != null))
523529
{
524530
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
525531
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/{status.Key}")
@@ -531,7 +537,7 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
531537
}
532538

533539
// Hold status
534-
foreach (var holdStatus in thermostatStatus.ActiveHold)
540+
foreach (var holdStatus in thermostatStatus.ActiveHold.Where(x => x.Value != null))
535541
{
536542
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
537543
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/hold/{holdStatus.Key}")
@@ -544,7 +550,7 @@ await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
544550

545551
foreach (var sensor in thermostatStatus.Sensors)
546552
{
547-
foreach (var sensorCapability in sensor.Value)
553+
foreach (var sensorCapability in sensor.Value.Where(x => x.Value != null))
548554
{
549555
await MqttClient.PublishAsync(new MqttApplicationMessageBuilder()
550556
.WithTopic($"{TopicRoot}/{thermostat.Identifier}/sensor/{sensor.Key.Sluggify()}/{sensorCapability.Key}")

src/HomeAutio.Mqtt.Ecobee/HomeAutio.Mqtt.Ecobee.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<PackageReference Include="I8Beef.CodeAnalysis.RuleSet" Version="1.0.15">
3434
<PrivateAssets>all</PrivateAssets>
3535
</PackageReference>
36-
<PackageReference Include="HomeAutio.Mqtt.Core" Version="3.0.0.65" />
37-
<PackageReference Include="I8Beef.Ecobee" Version="2.2.0.72" />
36+
<PackageReference Include="HomeAutio.Mqtt.Core" Version="3.0.0.67" />
37+
<PackageReference Include="I8Beef.Ecobee" Version="3.0.0.83" />
3838
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
3939
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
4040
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />

0 commit comments

Comments
 (0)