diff --git a/HAClimateDeskband/HAClimateDeskband.csproj b/HAClimateDeskband/HAClimateDeskband.csproj index 558bebe..3459825 100644 --- a/HAClimateDeskband/HAClimateDeskband.csproj +++ b/HAClimateDeskband/HAClimateDeskband.csproj @@ -69,7 +69,9 @@ HAClimateUserControl.cs + + Component diff --git a/HAClimateDeskband/HAClimateUserControl.cs b/HAClimateDeskband/HAClimateUserControl.cs index fedae84..5865ee9 100644 --- a/HAClimateDeskband/HAClimateUserControl.cs +++ b/HAClimateDeskband/HAClimateUserControl.cs @@ -1,4 +1,5 @@ using HAClimateDeskband.Models; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OxyPlot; using OxyPlot.Axes; @@ -6,6 +7,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Globalization; using System.IO; using System.Linq; using System.Net.Http; @@ -23,8 +25,9 @@ public partial class HAClimateUserControl : UserControl private PlotViewTransparent PlotViewTemperature { get; set; } private HttpClient HttpClient { get; set; } private HAClimateDeskBandSettings HAClimateDeskBandSettings { get; set; } - private Label LblMeasurePowerWidth; - private bool Initialized; + private readonly Label LblMeasurePowerWidth; + private readonly bool Initialized; + private readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { Culture = new CultureInfo("en-US") }; const int WindowsTaskbarSmallIconsSingleRow = 30; const int WindowsTaskbarSmallIconsDoubleRow = WindowsTaskbarSmallIconsSingleRow * 2; @@ -194,7 +197,13 @@ private void SetTemperature(double temperatureDelta) double currentSetTemperature = climateGarageState.SelectToken(".attributes.temperature").Value(); double newSetTemperature = currentSetTemperature += temperatureDelta; - HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/set_temperature", new StringContent($"{{ \"entity_id\": \"{HAClimateDeskBandSettings.ClimateEntityId}\", \"temperature\": {newSetTemperature} }}")).GetAwaiter().GetResult(); + + HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/set_temperature", new StringContent(JsonConvert.SerializeObject(new SetTemperatureModel + { + ClimateEntityId = HAClimateDeskBandSettings.ClimateEntityId, + Temperature = newSetTemperature + }, JsonSerializerSettings))).GetAwaiter().GetResult(); + httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } catch (Exception ex) @@ -432,7 +441,10 @@ private void SetClimate(bool on) { string command = on ? "turn_on" : "turn_off"; - HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/{command}", new StringContent($"{{ \"entity_id\": \"{HAClimateDeskBandSettings.ClimateEntityId}\" }}")).GetAwaiter().GetResult(); + HttpResponseMessage httpResponseMessage = HttpClient.PostAsync($"services/climate/{command}", new StringContent(JsonConvert.SerializeObject(new EntityModel + { + ClimateEntityId = HAClimateDeskBandSettings.ClimateEntityId + }, JsonSerializerSettings))).GetAwaiter().GetResult(); httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } catch (Exception ex) diff --git a/HAClimateDeskband/Models/EntityModel.cs b/HAClimateDeskband/Models/EntityModel.cs new file mode 100644 index 0000000..1a69dc5 --- /dev/null +++ b/HAClimateDeskband/Models/EntityModel.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace HAClimateDeskband.Models +{ + public class EntityModel + { + [JsonProperty("entity_id")] + public string ClimateEntityId { get; set; } + } +} diff --git a/HAClimateDeskband/Models/SetTemperatureModel.cs b/HAClimateDeskband/Models/SetTemperatureModel.cs new file mode 100644 index 0000000..137a56d --- /dev/null +++ b/HAClimateDeskband/Models/SetTemperatureModel.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace HAClimateDeskband.Models +{ + public class SetTemperatureModel : EntityModel + { + [JsonProperty("temperature")] + public double Temperature { get; set; } + } +}