2025-02-02 00:46:20 -05:00
|
|
|
using System;
|
|
|
|
|
|
|
|
using HomeAssistantGenerated;
|
|
|
|
|
|
|
|
using NetDaemon.AppModel;
|
|
|
|
using NetDaemon.HassModel.Entities;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NetDaemonConfig.Apps.Frontend.BathroomLight
|
|
|
|
{
|
|
|
|
[NetDaemonApp]
|
|
|
|
public class BathroomLight
|
|
|
|
{
|
2025-02-02 16:08:03 -05:00
|
|
|
// The range of the brightness is 0 to 254
|
|
|
|
private const double CONV_RATE = 2.54;
|
|
|
|
|
2025-02-02 12:48:20 -05:00
|
|
|
private LightEntity? Entity { get; set; }
|
|
|
|
private InputTextEntity? BrightnessInput { get; set; }
|
2025-02-02 16:08:34 -05:00
|
|
|
private InputTextEntity? TemperatureInput { get; set; }
|
2025-02-02 12:14:44 -05:00
|
|
|
|
2025-02-02 12:48:20 -05:00
|
|
|
private void BrightnessCallback(Action<double, double> callback)
|
|
|
|
{
|
|
|
|
double? currentBrightness = this.Entity?.Attributes?.Brightness;
|
2025-02-02 00:46:20 -05:00
|
|
|
|
2025-02-02 12:48:20 -05:00
|
|
|
if (currentBrightness is not null)
|
2025-02-02 00:46:20 -05:00
|
|
|
{
|
2025-02-02 16:08:03 -05:00
|
|
|
double currentBrightnessPercent = Math.Floor((double)currentBrightness / CONV_RATE);
|
2025-02-02 12:48:20 -05:00
|
|
|
double inputBrightnessPercent = double.Parse(this.BrightnessInput?.State ?? "0");
|
2025-02-02 00:46:20 -05:00
|
|
|
|
2025-02-02 12:48:20 -05:00
|
|
|
if (currentBrightnessPercent != inputBrightnessPercent)
|
2025-02-02 00:46:20 -05:00
|
|
|
{
|
2025-02-02 12:48:20 -05:00
|
|
|
callback(currentBrightnessPercent, inputBrightnessPercent);
|
2025-02-02 00:46:20 -05:00
|
|
|
}
|
2025-02-02 12:48:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-02 16:08:34 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-02 12:48:20 -05:00
|
|
|
public BathroomLight(Services services, Entities entities)
|
|
|
|
{
|
2025-02-02 16:08:03 -05:00
|
|
|
this.Entity = entities.Light.Bathroomceiling;
|
2025-02-02 00:46:20 -05:00
|
|
|
|
2025-02-02 12:48:20 -05:00
|
|
|
this.BrightnessInput = entities.InputText.BathroomLightBrightness;
|
2025-02-02 16:08:34 -05:00
|
|
|
this.TemperatureInput = entities.InputText.BathroomLightTemperature;
|
2025-02-02 12:48:20 -05:00
|
|
|
|
2025-02-02 16:08:03 -05:00
|
|
|
|
|
|
|
// Brightness
|
2025-02-02 12:48:20 -05:00
|
|
|
this.BrightnessInput
|
2025-02-02 00:46:20 -05:00
|
|
|
.StateAllChanges()
|
2025-02-02 12:48:20 -05:00
|
|
|
.Subscribe((_) =>
|
|
|
|
this.BrightnessCallback((_, input) =>
|
|
|
|
services.Light.TurnOn(
|
2025-02-02 16:08:03 -05:00
|
|
|
target: ServiceTarget.FromEntity(this.Entity.EntityId),
|
|
|
|
brightness: Math.Floor(input * CONV_RATE + 1))));
|
2025-02-02 00:46:20 -05:00
|
|
|
|
2025-02-02 12:48:20 -05:00
|
|
|
this.Entity
|
2025-02-02 00:46:20 -05:00
|
|
|
.StateAllChanges()
|
2025-02-02 12:48:20 -05:00
|
|
|
.Subscribe((_) =>
|
|
|
|
this.BrightnessCallback((current, _) =>
|
|
|
|
this.BrightnessInput.SetValue(current.ToString())));
|
2025-02-02 00:46:20 -05:00
|
|
|
|
|
|
|
// Init value
|
2025-02-02 12:48:20 -05:00
|
|
|
this.BrightnessCallback((current, _) =>
|
|
|
|
this.BrightnessInput.SetValue(current.ToString()));
|
2025-02-02 16:08:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
// 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()));
|
2025-02-02 00:46:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|