zhbjsh / homeassistant-ssh

Control and monitor devices in Home Assistant by executing commands via SSH
MIT License
34 stars 3 forks source link

Systemd switch not working as expected #10

Open dapuzz opened 5 months ago

dapuzz commented 5 months ago

Hi Adrian, Copying your example systemctl -a | awk '/hyperion.service|pure-ftpd.service/ {print $1 "," $4}' create the switch just when the service is running. As soon as the switch is turned off, it became unavaiable I suppose because the systemctl -a does not return anything if the service is not active.

2024-04-14 00:32:27.306 DEBUG (MainThread) [custom_components.ssh] raspberrypi: systemctl -a | awk '/kodi.service|hyperion.service|drm-capture.service|pure-ftpd.service/ {print $1 "," $4}' => ['hyperion.service,running'], [], 0
2024-04-14 00:32:27.307 DEBUG (MainThread) [custom_components.ssh] raspberrypi: service_hyperion_service => True
2024-04-14 00:32:27.307 DEBUG (MainThread) [custom_components.ssh] Finished fetching raspberrypi service data in 0.037 seconds (success: True)

image

Do you have any suggestion? Thank you

zhbjsh commented 4 weeks ago

Hi!

If systemctl -a doesnt list inactive services, you could create a static switch for each service, maybe you could try something like this:

- command: systemctl is-active kodi.service || true
  scan_interval: 300
  sensors:
    - type: binary
      name: Kodi
      command_on: systemctl start kodi.service
      command_off: systemctl stop kodi.service
      payload_on: active

Let me know if it works for you!