Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for input volume #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions mac2mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ func getCurrentVolume() int {
return i
}

func getCurrentInputVolume() int {
input := getCommandOutput("/usr/bin/osascript", "-e", "input volume of (get volume settings)")

i, err := strconv.Atoi(input)
if err != nil {
log.Fatal(err)
}

return i
}

func runCommand(name string, arg ...string) {
cmd := exec.Command(name, arg...)

Expand All @@ -127,6 +138,11 @@ func setVolume(i int) {
runCommand("/usr/bin/osascript", "-e", "set volume output volume "+strconv.Itoa(i))
}

// from 0 to 100
func setInputVolume(i int) {
runCommand("/usr/bin/osascript", "-e", "set volume input volume "+strconv.Itoa(i))
}

// true - turn mute on
// false - turn mute off
func setMute(b bool) {
Expand Down Expand Up @@ -215,6 +231,22 @@ func listen(client mqtt.Client, topic string) {

}

if msg.Topic() == getTopicPrefix()+"/command/input_volume" {

i, err := strconv.Atoi(string(msg.Payload()))
if err == nil && i >= 0 && i <= 100 {

setInputVolume(i)

updateInputVolume(client)
// updateMute(client)

} else {
log.Println("Incorrect value")
}

}

if msg.Topic() == getTopicPrefix()+"/command/mute" {

b, err := strconv.ParseBool(string(msg.Payload()))
Expand Down Expand Up @@ -267,6 +299,12 @@ func updateVolume(client mqtt.Client) {
token.Wait()
}

func updateInputVolume(client mqtt.Client) {
token := client.Publish(getTopicPrefix()+"/status/input_volume", 0, false, strconv.Itoa(getCurrentInputVolume()))
token.Wait()
}


func updateMute(client mqtt.Client) {
token := client.Publish(getTopicPrefix()+"/status/mute", 0, false, strconv.FormatBool(getMuteStatus()))
token.Wait()
Expand Down Expand Up @@ -312,6 +350,7 @@ func main() {
select {
case _ = <-volumeTicker.C:
updateVolume(mqttClient)
updateInputVolume(mqttClient)
updateMute(mqttClient)

case _ = <-batteryTicker.C:
Expand Down