refactor(netd): clean up code
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2025-02-02 12:48:20 -05:00
parent 70fe1a0e81
commit f24026db00
2 changed files with 37 additions and 38 deletions

View file

@ -26,10 +26,10 @@ dotnet_sort_system_directives_first = true
file_header_template = unset file_header_template = unset
# this. and Me. preferences # this. and Me. preferences
dotnet_style_qualification_for_event = false:silent dotnet_style_qualification_for_event = true
dotnet_style_qualification_for_field = false:silent dotnet_style_qualification_for_field = true
dotnet_style_qualification_for_method = false:silent dotnet_style_qualification_for_method = true
dotnet_style_qualification_for_property = false:silent dotnet_style_qualification_for_property = true
# Language keywords vs BCL types preferences # Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:silent dotnet_style_predefined_type_for_locals_parameters_members = true:silent

View file

@ -11,51 +11,50 @@ namespace NetDaemonConfig.Apps.Frontend.BathroomLight
[NetDaemonApp] [NetDaemonApp]
public class BathroomLight public class BathroomLight
{ {
private LightEntity? Entity { get; set; }
private InputTextEntity? BrightnessInput { get; set; }
private void BrightnessCallback(Action<double, double> callback)
{
double? currentBrightness = this.Entity?.Attributes?.Brightness;
if (currentBrightness is not null)
{
double currentBrightnessPercent = Math.Floor((double)currentBrightness / 2.54);
double inputBrightnessPercent = double.Parse(this.BrightnessInput?.State ?? "0");
if (currentBrightnessPercent != inputBrightnessPercent)
{
callback(currentBrightnessPercent, inputBrightnessPercent);
}
}
}
public BathroomLight(Services services, Entities entities) public BathroomLight(Services services, Entities entities)
{ {
// ZigBee needs restart to access Light // ZigBee needs restart to access Light
entities.Button.Slzb06p7CoreRestart.Press(); entities.Button.Slzb06p7CoreRestart.Press();
LightEntity? bathroomLight = entities.Light.Tz3210KatchgxyTs0505bLight; this.Entity = entities.Light.Tz3210KatchgxyTs0505bLight;
InputTextEntity? bathroomLightBrightness = entities.InputText.BathroomLightBrightness; this.BrightnessInput = entities.InputText.BathroomLightBrightness;
var brightnessCallback = (Action<double, double> callback) => this.BrightnessInput
{
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() .StateAllChanges()
.Subscribe((_) => brightnessCallback((current, input) => .Subscribe((_) =>
{ this.BrightnessCallback((_, input) =>
services.Light.TurnOn( services.Light.TurnOn(
target: ServiceTarget.FromEntity("light.tz3210_katchgxy_ts0505b_light"), target: ServiceTarget.FromEntity("light.tz3210_katchgxy_ts0505b_light"),
brightness: Math.Floor(input * 2.54 + 1)); brightness: Math.Floor(input * 2.54 + 1))));
}));
bathroomLight this.Entity
.StateAllChanges() .StateAllChanges()
.Subscribe((_) => brightnessCallback((current, input) => .Subscribe((_) =>
{ this.BrightnessCallback((current, _) =>
bathroomLightBrightness.SetValue(current.ToString()); this.BrightnessInput.SetValue(current.ToString())));
}));
// Init value // Init value
brightnessCallback((current, input) => this.BrightnessCallback((current, _) =>
{ this.BrightnessInput.SetValue(current.ToString()));
bathroomLightBrightness.SetValue(current.ToString());
});
} }
} }
} }