From b9402f46437a401ed16cc4e5c096e360735a67f3 Mon Sep 17 00:00:00 2001
From: matt1432 <matt@nelim.org>
Date: Sun, 2 Feb 2025 16:08:34 -0500
Subject: [PATCH] feat(netd): add temperature input_text

---
 .../homie/modules/home-assistant/frontend.nix |  6 +++
 .../Frontend/BathroomLight/BathroomLight.cs   | 37 +++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/configurations/homie/modules/home-assistant/frontend.nix b/configurations/homie/modules/home-assistant/frontend.nix
index dd1285ac..a7efbc19 100644
--- a/configurations/homie/modules/home-assistant/frontend.nix
+++ b/configurations/homie/modules/home-assistant/frontend.nix
@@ -133,6 +133,12 @@ in {
         initial = "0";
         max = 3;
       };
+
+      bathroom_light_temperature = {
+        name = "BathroomLightTemperature";
+        pattern = "[0-9]*";
+        initial = "0";
+      };
     };
 
     lovelaceConfig = {
diff --git a/configurations/homie/modules/home-assistant/netdaemon/apps/Frontend/BathroomLight/BathroomLight.cs b/configurations/homie/modules/home-assistant/netdaemon/apps/Frontend/BathroomLight/BathroomLight.cs
index 7e386c9d..2156b6fc 100644
--- a/configurations/homie/modules/home-assistant/netdaemon/apps/Frontend/BathroomLight/BathroomLight.cs
+++ b/configurations/homie/modules/home-assistant/netdaemon/apps/Frontend/BathroomLight/BathroomLight.cs
@@ -16,6 +16,7 @@ namespace NetDaemonConfig.Apps.Frontend.BathroomLight
 
         private LightEntity? Entity { get; set; }
         private InputTextEntity? BrightnessInput { get; set; }
+        private InputTextEntity? TemperatureInput { get; set; }
 
         private void BrightnessCallback(Action<double, double> callback)
         {
@@ -33,11 +34,27 @@ namespace NetDaemonConfig.Apps.Frontend.BathroomLight
             }
         }
 
+        private void TemperatureCallback(Action<double, double> callback)
+        {
+            double? currentTemperature = this.Entity?.Attributes?.ColorTempKelvin;
+
+            if (currentTemperature is not null)
+            {
+                double inputTemperature = double.Parse(this.TemperatureInput?.State ?? "0");
+
+                if (currentTemperature != inputTemperature)
+                {
+                    callback((double)currentTemperature, inputTemperature);
+                }
+            }
+        }
+
         public BathroomLight(Services services, Entities entities)
         {
             this.Entity = entities.Light.Bathroomceiling;
 
             this.BrightnessInput = entities.InputText.BathroomLightBrightness;
+            this.TemperatureInput = entities.InputText.BathroomLightTemperature;
 
 
             // Brightness
@@ -58,6 +75,26 @@ namespace NetDaemonConfig.Apps.Frontend.BathroomLight
             // Init value
             this.BrightnessCallback((current, _) =>
                 this.BrightnessInput.SetValue(current.ToString()));
+
+
+            // Temperature
+            this.TemperatureInput
+                .StateAllChanges()
+                .Subscribe((_) =>
+                    this.TemperatureCallback((_, input) =>
+                        services.Light.TurnOn(
+                            target: ServiceTarget.FromEntity(this.Entity.EntityId),
+                            kelvin: input)));
+
+            this.Entity
+                .StateAllChanges()
+                .Subscribe((_) =>
+                    this.TemperatureCallback((current, _) =>
+                        this.TemperatureInput.SetValue(current.ToString())));
+
+            // Init value
+            this.TemperatureCallback((current, _) =>
+                this.TemperatureInput.SetValue(current.ToString()));
         }
     }
 }