Skip to content

Commit

Permalink
add sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
schmic committed Dec 10, 2023
1 parent 88d2857 commit aeaf2f9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
74 changes: 74 additions & 0 deletions HaPlugin/Actions/SensorCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace Loupedeck.HomeAssistant
{
using System;
using System.Collections.Generic;

using Loupedeck.HomeAssistant.Events;

public class SensorCommand : PluginDynamicCommand
{
protected BitmapColor BgColor_off = new BitmapColor(22, 33, 38);
protected BitmapColor BgColor_on = new BitmapColor(73, 27, 74);

protected HaPlugin plugin;

public SensorCommand() : base() { }

protected override Boolean OnLoad()
{
this.plugin = (HaPlugin)base.Plugin;

this.plugin.StatesReady += this.StatesReady;
this.plugin.StateChanged += this.StateChanged;

return true;
}

protected override Boolean OnUnload()
{
this.plugin.StateChanged -= this.StateChanged;
this.plugin.StatesReady -= this.StatesReady;

return true;
}

private void StatesReady(Object sender, EventArgs e)
{
PluginLog.Verbose($"SensorCommand.OnLoad() => StatesReady");

foreach (KeyValuePair<String, Json.HaState> kvp in this.plugin.States)
{
if (!kvp.Key.StartsWith($"sensor.") && !kvp.Key.StartsWith($"binary_sensor."))
{ continue; }

PluginLog.Verbose($"{kvp.Key} is a sensor with {kvp.Value}");


var state = kvp.Value;
this.AddParameter(state.Entity_Id, state.FriendlyName, "Status");
}

PluginLog.Info($"Sensor => {this.GetParameters().Length} found.");
}

private void StateChanged(Object sender, StateChangedEventArgs e)
{
if (!e.Entity_Id.StartsWith($"sensor.") && !e.Entity_Id.StartsWith($"binary_sensor."))
{ return; }

this.ActionImageChanged(e.Entity_Id);
}

protected override String GetCommandDisplayName(String entity_id, PluginImageSize imageSize)
{
if (entity_id.IsNullOrEmpty())
{ return base.GetCommandDisplayName(entity_id, imageSize); }

var FriendlyName = this.plugin.States[entity_id].FriendlyName;
var State = this.plugin.States[entity_id].State;
var Unit_Of_Measurement = this.plugin.States[entity_id].Attributes["unit_of_measurement"];

return $"{FriendlyName}\n{State}{Unit_Of_Measurement}";
}
}
}
1 change: 1 addition & 0 deletions HaPlugin/HomeAssistant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<ItemGroup>
<Compile Include="Actions\ClimateAdjustment.cs" />
<Compile Include="Actions\DimmerAdjustment.cs" />
<Compile Include="Actions\SensorCommand.cs" />
<Compile Include="Actions\DualStateCommand.cs" />
<Compile Include="Actions\SwitchCommand.cs" />
<Compile Include="Actions\LightCommand.cs" />
Expand Down

0 comments on commit aeaf2f9

Please sign in to comment.