diff --git a/configurations/homie/modules/home-assistant/frontend.nix b/configurations/homie/modules/home-assistant/frontend.nix index 319aca27..45ba3792 100644 --- a/configurations/homie/modules/home-assistant/frontend.nix +++ b/configurations/homie/modules/home-assistant/frontend.nix @@ -124,6 +124,14 @@ in { } ]; + # TODO: restrict to 0-100 + config.input_text = { + bathroom_light_brightness = { + name = "BathroomLightBrightness"; + pattern = "[0-9]*"; + }; + }; + lovelaceConfig = { title = "Our House"; # I don't want multiple views diff --git a/configurations/homie/modules/home-assistant/netdaemon/HomeAssistantGenerated b/configurations/homie/modules/home-assistant/netdaemon/HomeAssistantGenerated index 6fd6f050..ca915f35 100644 Binary files a/configurations/homie/modules/home-assistant/netdaemon/HomeAssistantGenerated and b/configurations/homie/modules/home-assistant/netdaemon/HomeAssistantGenerated differ 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 new file mode 100644 index 00000000..60a774f2 --- /dev/null +++ b/configurations/homie/modules/home-assistant/netdaemon/apps/Frontend/BathroomLight/BathroomLight.cs @@ -0,0 +1,58 @@ +using System; + +using HomeAssistantGenerated; + +using NetDaemon.AppModel; +using NetDaemon.HassModel.Entities; + + +namespace NetDaemonConfig.Apps.Frontend.BathroomLight +{ + [NetDaemonApp] + public class BathroomLight + { + public BathroomLight(Services services, Entities entities) + { + LightEntity? bathroomLight = entities.Light.Tz3210KatchgxyTs0505bLight; + InputTextEntity? bathroomLightBrightness = entities.InputText.BathroomLightBrightness; + + var brightnessCallback = (Action callback) => + { + double? currentBrightness = bathroomLight?.Attributes?.Brightness; + + if (currentBrightness is not null) + { + double currentBrightnessPercent = Math.Floor((double)currentBrightness / 2.54); + double inputBrightnessPercent = Double.Parse(bathroomLightBrightness.State ?? "0"); + + if (currentBrightnessPercent != inputBrightnessPercent) + { + callback(currentBrightnessPercent, inputBrightnessPercent); + } + } + }; + + bathroomLightBrightness + .StateAllChanges() + .Subscribe((_) => brightnessCallback((current, input) => + { + services.Light.TurnOn( + target: ServiceTarget.FromEntity("light.tz3210_katchgxy_ts0505b_light"), + brightness: Math.Floor(input * 2.54 + 1)); + })); + + bathroomLight + .StateAllChanges() + .Subscribe((_) => brightnessCallback((current, input) => + { + bathroomLightBrightness.SetValue(current.ToString()); + })); + + // Init value + brightnessCallback((current, input) => + { + bathroomLightBrightness.SetValue(current.ToString()); + }); + } + } +}