ironcrafter54 / mac2mqtt

mac2mqtt is a program that allow viewing and controlling some aspects of computers running macOS via MQTT.
MIT License
8 stars 0 forks source link

Mac2MQTT (updated)

This was a fun project, but I have now realized you should just use the Home Assistant SSH integration it's better in most ways, you can add controls more easily, it can execute any command, and is more reliable, and you don't need to run an additional piece of software.

mac2mqtt is a program that allows viewing and controlling some aspects of computers running macOS via MQTT.

This repo is a fork of bessarabov/mac2mqtt, that fixes bugs and adds features.

It publishes to MQTT:

You can send topics to:

Running

To run this program you need to put 2 files in a directory (/Users/USERNAME/mac2mqtt/):

mac2mqtt
mac2mqtt.yaml

Edit mac2mqtt.yaml (the sample file is in this repository), make binary executable (chmod +x mac2mqtt) and run ./mac2mqtt:

$ ./mac2mqtt
2021/04/12 10:37:28 Started
2021/04/12 10:37:29 Connected to MQTT
2021/04/12 10:37:29 Sending 'true' to topic: mac2mqtt/bessarabov-osx/status/alive

Running in the background

You need mac2mqtt.yaml and mac2mqtt to be placed in the directory /Users/USERNAME/mac2mqtt/, then you need edit the file com.bessarabov.mac2mqtt.plist and replace USERNAME with your username. Then put the file in /Library/LaunchAgents/.

And run:

launchctl load /Library/LaunchAgents/com.bessarabov.mac2mqtt.plist

(To stop you need to run launchctl unload /Library/LaunchAgents/com.bessarabov.mac2mqtt.plist)

Home Assistant sample config

configuration.yaml:

script:
  air2_sleep:
    icon: mdi:laptop
    sequence:
      - service: mqtt.publish
        data:
          topic: "mac2mqtt/bessarabov-osx/command/sleep"
          payload: "sleep"

  air2_shutdown:
    icon: mdi:laptop
    sequence:
      - service: mqtt.publish
        data:
          topic: "mac2mqtt/bessarabov-osx/command/shutdown"
          payload: "shutdown"

  air2_displaysleep:
    icon: mdi:laptop
    sequence:
      - service: mqtt.publish
        data:
          topic: "mac2mqtt/bessarabov-osx/command/displaysleep"
          payload: "displaysleep"

sensor:
  - platform: mqtt
    name: air2_alive
    icon: mdi:laptop
    state_topic: "mac2mqtt/bessarabov-osx/status/alive"

  - platform: mqtt
    name: "air2_battery"
    icon: mdi:battery-high
    unit_of_measurement: "%"
    state_topic: "mac2mqtt/bessarabov-osx/status/battery"

switch:
  - platform: mqtt
    name: air2_mute
    icon: mdi:volume-mute
    state_topic: "mac2mqtt/bessarabov-osx/status/mute"
    command_topic: "mac2mqtt/bessarabov-osx/command/mute"
    payload_on: "true"
    payload_off: "false"

number:
  - platform: mqtt
    name: air2_volume
    icon: mdi:volume-medium
    state_topic: "mac2mqtt/bessarabov-osx/status/volume"
    command_topic: "mac2mqtt/bessarabov-osx/command/volume"

ui-lovelace.yaml:

script:
  air2_sleep:
    icon: mdi:laptop
    sequence:
      - service: mqtt.publish
        data:
          topic: "mac2mqtt/bessarabov-osx/command/sleep"
          payload: "sleep"

  air2_shutdown:
    icon: mdi:laptop
    sequence:
      - service: mqtt.publish
        data:
          topic: "mac2mqtt/bessarabov-osx/command/shutdown"
          payload: "shutdown"

  air2_displaysleep:
    icon: mdi:laptop
    sequence:
      - service: mqtt.publish
        data:
          topic: "mac2mqtt/bessarabov-osx/command/displaysleep"
          payload: "displaysleep"

mqtt:
  sensor:
    - name: air2_alive
      icon: mdi:laptop
      state_topic: "mac2mqtt/bessarabov-osx/status/alive"

    - name: "air2_battery"
      icon: mdi:battery-high
      unit_of_measurement: "%"
      state_topic: "mac2mqtt/bessarabov-osx/status/battery"

  switch:
    - name: air2_mute
      icon: mdi:volume-mute
      state_topic: "mac2mqtt/bessarabov-osx/status/mute"
      command_topic: "mac2mqtt/bessarabov-osx/command/mute"
      payload_on: "true"
      payload_off: "false"

  number:
    - name: air2_volume
      icon: mdi:volume-medium
      state_topic: "mac2mqtt/bessarabov-osx/status/volume"
      command_topic: "mac2mqtt/bessarabov-osx/command/volume"

MQTT topics structure

The program is working with several MQTT topics. All topics are prefixed with mac2mqtt + COMPUTER_NAME. For example, the topic with the current volume on my machine is mac2mqtt/bessarabov-osx/status/volume

mac2mqtt send info to the topics mac2mqtt/COMPUTER_NAME/status/# and listen for commands in topics mac2mqtt/COMPUTER_NAME/command/#.

PREFIX + /status/alive

There can be true or false in this topic. If mac2mqtt is connected to MQTT server there is true. If mac2mqtt is disconnected from MQTT there is false. This is the standard MQTT thing called Last Will and Testament.

PREFIX + /status/volume

The value ranges from 0 (inclusive) to 100 (inclusive)—the current volume of the computer.

The value of this topic is updated every 60 seconds.

PREFIX + /status/mute

There can be true or false in this topic. true means that the computer volume is muted (no sound), false means that it is not muted.

PREFIX + /status/battery

The value ranges from 0 (inclusive) to 100 (inclusive) and represents the current level of the battery. Returns empty if there is no battery.

The value of this topic is updated every 60 seconds.

PREFIX + /command/volume

You can send integer numbers from 0 (inclusive) to 100 (inclusive) to this topic. It will set the volume on the computer.

PREFIX + /command/mute

You can send true or false to this topic. When you send true the computer is muted. When you send false the computer is unmuted.

PREFIX + /command/sleep

You can send sleep to this topic, and it will put the computer to sleep. Sending other values will do nothing.

PREFIX + /command/shutdown

You can send shutdown to this topic. It will try to shut down the computer. The way it is done depends on the user who ran the program. If the program is run by root the computer will shut down, but if it is run by an ordinary user the computer will not shut down if there is another user who logged in.

Sending some other value but shutdown will do nothing.

PREFIX + /command/displaysleep

You can send displaysleep to this topic. It will turn off the display. Sending some other value will do nothing.

PREFIX + /command/runshortcut

You can send the name of a shortcut to this topic. It will run this shortcut in the Shortcuts app.

PREFIX + /command/displaywake

You can send displaywake to this topic. It will turn on the display. Sending some other value will do nothing.

PREFIX + /command/screensaver

You can send screensaver to this topic. It will turn start your screensaver. Sending some other value will do nothing.

Building

To build this program yourself, follow these steps:

  1. Clone this repo
  2. Make sure you have installed go, for example with brew install go
  3. Install its dependencies with go install
  4. Build with go build mac2mqtt.go

It outputs a file mac2mqtt. Make the binary executable (chmod +x mac2mqtt) and run ./mac2mqtt.