halinuxcompanion icon indicating copy to clipboard operation
halinuxcompanion copied to clipboard

HomeAssistant Linux Companion

Home Assistant Linux Companion

Application to run on Linux desktop computer to provide sensors data to Home Assistant, and get notifications as if it was a mobile device.

How To

Requirements

  1. Python 3.10
  2. requirements.txt

Instructions

  1. Get a long-lived access token from your Home Assistant user
  2. Install requirements pip3 install -r requirements.txt
  3. Modify config.json to match your setup and desired options.
  4. Run the application python3 -m halinuxcompanion --config config.json
    • Alternative set up a systemd service using the provided unit file

Now in your Home Assistant you will see a new device in the "mobile_app" integration, and there will be a new service to notify your Linux desktop. Notification actions work and the expected events will be fired in Home Assistant.

Example configuration file

{
  "ha_url": "http://homeassistant.local:8123/",
  "ha_token": "mysuperlongtoken",
  "device_id": "computername",
  "device_name": "whatever you want can be left empty",
  "manufacturer": "whatever you want can be left empty",
  "model": "Computer",
  "computer_ip": "192.168.1.15",
  "computer_port": 8400,
  "refresh_interval": "15",
  "loglevel": "INFO",
  "sensors": {
    "cpu": {
      "enabled": true,
      "name": "CPU"
    },
    "memory": {
      "enabled": true,
      "name": "Memory Load"
    },
    "uptime": {
      "enabled": true,
      "name": "Uptime"
    },
    "status": {
      "enabled": true,
      "name": "Status"
    },
    "battery_level": {
      "enabled": true,
      "name": "Battery Level"
    },
    "battery_state": {
      "enabled": true,
      "name": "Battery State"
    }
  },
  "services": {
    "notifications": {
      "enabled": true,
      "url_program": "xdg-open",
      "commands": {
        "command_suspend": {
          "name": "Suspend",
          "command": ["systemctl", "suspend"]
        },
        "command_poweroff": {
          "name": "Power off",
          "command": ["systemctl", "poweroff"]
        },
        "command_reboot": {
          "name": "Reboot",
          "command": ["systemctl", "reboot"]
        },
        "command_hibernate": {
          "name": "Hibernate",
          "command": ["systemctl", "hibernate"]
        },
        "command_open_ha": {
          "name": "Open Home Assistant",
          "command": ["xdg-open", "http://homeassistant.local:8123/"]
        },
        "command_open_spotify": {
          "name": "Open Spotify Flatpak",
          "command": ["flatpak", "run", "com.spotify.Client"]
        }
      }
    }
  }
}

Technical

To-do

  • [ ] Implement encryption
  • [ ] Move sensors to MQTT
    The reasoning for the change is the limitations of the API, naturally is expected that desktop and laptops would go offline and I would like for the sensors to reflect this new state. But if for some reason the application is unable to send this new state to Home Assistant the values of the sensors would be stuck. But if the app uses MQTT it can set will topics for the sensors to be updated when the client can't communicate with the server.
  • [ ] One day make it work with remote and local instance, for laptops roaming networks
  • [x] Status sensors that listens to sleep, wakeup, shutdown, power_on
  • [ ] Add more sensors
  • [ ] Finish notifications functionality

Features

  • Sensors:
    • CPU
    • Memory
    • Uptime
    • Status: Computer status, reflects if the computer went to sleep, wakes up, shutdown, turned on. The sensor is updated right before any of these events happen by listening to dbus signals.
    • Battery Level
    • Batter State
  • Notifications:
  • Default commands (example config):
    • Suspend
    • Power off
    • Reboot
    • Hibernate