feat(hass): add synced input_text for brightness control
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2025-02-02 00:46:20 -05:00
parent 9f41b0025b
commit bb4a8791fc
3 changed files with 66 additions and 0 deletions

View file

@ -124,6 +124,14 @@ in {
} }
]; ];
# TODO: restrict to 0-100
config.input_text = {
bathroom_light_brightness = {
name = "BathroomLightBrightness";
pattern = "[0-9]*";
};
};
lovelaceConfig = { lovelaceConfig = {
title = "Our House"; title = "Our House";
# I don't want multiple views # I don't want multiple views

View file

@ -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<double, double> 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());
});
}
}
}