Merge pull request #163 from calielc/use-button-entities-for-commands

Use button entities for command
pull/188/head
sleevezipper 3 years ago committed by GitHub
commit f4eaaaecdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -40,6 +40,7 @@ If a class or value cannot be found in the default scope, you can use the "Scope
| Logical Processors | `SELECT NumberOfLogicalProcessors FROM Win32_ComputerSystem` | `8` (Processors * Cores * Threads) | [:link:](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem) | @deftnerd |
| System Type | `SELECT PCSystemType FROM Win32_ComputerSystem` | `1` (0=Unspecified, 1=Desktop, 2=Laptop, 3=Workstation, etc) | [:link:](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem) | @deftnerd |
| Processor Architecture | `SELECT SystemType FROM Win32_ComputerSystem` | `64-bit Intel PC` | [:link:](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem) | @deftnerd |
| Free Disk Space | `SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID='C:'` | `299329323008` (bytes) | [:link:](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk) | @sleevezipper |
### Power & Thermal Information
| Data | Query | Example Response | Info | Contributor |

@ -219,13 +219,11 @@ namespace hass_workstation_service.Communication
{
if (((CommandDiscoveryConfigModel)command.GetAutoDiscoveryConfig()).Command_topic == applicationMessage.Topic)
{
if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "ON")
switch (Encoding.UTF8.GetString(applicationMessage?.Payload))
{
command.TurnOn();
}
else if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "OFF")
{
command.TurnOff();
case "PRESS":
command.Press();
break;
}
}

@ -14,7 +14,7 @@ namespace hass_workstation_service.Domain.Commands
public DateTime? LastUpdated { get; protected set; }
public string PreviousPublishedState { get; protected set; }
public MqttPublisher Publisher { get; protected set; }
public override string Domain { get => "switch"; }
public override string Domain { get => "button"; }
public AbstractCommand(MqttPublisher publisher, string name, Guid id = default)
{
@ -24,15 +24,13 @@ namespace hass_workstation_service.Domain.Commands
publisher.Subscribe(this);
}
public abstract string GetState();
public async Task PublishStateAsync()
{
// dont't even check the state if the update interval hasn't passed
if (LastUpdated.HasValue && LastUpdated.Value.AddSeconds(UpdateInterval) > DateTime.UtcNow)
return;
string state = GetState();
string state = "";
// don't publish the state if it hasn't changed
if (PreviousPublishedState == state)
return;
@ -59,7 +57,6 @@ namespace hass_workstation_service.Domain.Commands
return config;
}
public abstract void TurnOn();
public abstract void TurnOff();
public abstract void Press();
}
}

@ -12,17 +12,14 @@ namespace hass_workstation_service.Domain.Commands
public class CustomCommand : AbstractCommand
{
public string Command { get; protected set; }
public string State { get; protected set; }
public Process Process { get; private set; }
public CustomCommand(MqttPublisher publisher, string command, string name = "Custom", Guid id = default(Guid)) : base(publisher, name ?? "Custom", id)
{
this.Command = command;
this.State = "OFF";
}
public override async void TurnOn()
public override async void Press()
{
this.State = "ON";
this.Process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
@ -33,7 +30,6 @@ namespace hass_workstation_service.Domain.Commands
// turn off the sensor to guarantee disable the switch
// useful if command changes power state of device
this.State = "OFF";
try
{
@ -42,7 +38,6 @@ namespace hass_workstation_service.Domain.Commands
catch (Exception e)
{
Log.Logger.Error($"Sensor {this.Name} failed", e);
this.State = "FAILED";
}
}
@ -61,15 +56,5 @@ namespace hass_workstation_service.Domain.Commands
Device = this.Publisher.DeviceConfigModel,
};
}
public override string GetState()
{
return this.State;
}
public override void TurnOff()
{
this.Process.Kill();
}
}
}

@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands
{
public HibernateCommand(MqttPublisher publisher, string name = "Hibernate", Guid id = default(Guid)) : base(publisher, "shutdown /h", name ?? "Hibernate", id)
{
this.State = "OFF";
}
}
}

@ -42,18 +42,7 @@ namespace hass_workstation_service.Domain.Commands
[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public override string GetState()
{
return "OFF";
}
public override void TurnOff()
{
}
public override void TurnOn()
public sealed override void Press()
{
keybd_event(this.KeyCode, 0, 0, IntPtr.Zero);
keybd_event(this.KeyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero);

@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands
{
public LogOffCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /l", name ?? "LogOff", id)
{
this.State = "OFF";
}
}
}

@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands
{
public RestartCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /r", name ?? "Restart", id)
{
this.State = "OFF";
}
}
}

@ -11,7 +11,6 @@ namespace hass_workstation_service.Domain.Commands
{
public ShutdownCommand(MqttPublisher publisher, string name = "Shutdown", Guid id = default(Guid)) : base(publisher, "shutdown /s", name ?? "Shutdown", id)
{
this.State = "OFF";
}
}
}

Loading…
Cancel
Save