#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import os
import yaml
import mpd
import paho.mqtt.client as mqtt
import time
from socket import error as socket_error

__author__ = "<NAME>"
__license__ = "DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 2004"
__version__ = "0.3"
__email__ = "<EMAIL>"

swd = os.path.dirname(os.path.abspath(__file__)) + '/'
with open(swd + "config.demo_mpd_volume_thermostat.yaml", 'r') as f:
	config = yaml.load(f)

prefix = config['mqtt']['prefix']

def on_mqtt_message(mqttc, obj, msg):

	# This condition is not needed since we **only subscribe to this single topic** (see the mqttc.subscribe call below),
	# however if we were to enhance this script's capabilities and subscribed to multiple topics,
	# this is here to remind us to filter the messages. 
	if (msg.topic == prefix + 'thermostat/set'):
		mpdc.setvol(int( 
				((float(msg.payload) - 6) / 34) * 100
		));	
		
def on_mqtt_disconnect(mqttc, userdata, rc):

	print("# Called on_disconnect!")
	while True:
		try:
			if (mqttc.reconnect() == 0):
				print("# Reconnected successfully.")
				break
		except socket_error:
			pass

		time.sleep(1)

def on_mqtt_connect(mqttc, userdata, flags, rc):
	mqttc.subscribe(prefix + 'thermostat/set', config['mqtt']['default_qos'])
		
# MPD client
mpdc = mpd.MPDClient(use_unicode=True)
mpdc.connect(config['mpd']['server'], config['mpd']['port'])

if (config['mpd']['password'] != ''):
	mpdc.password(config['mpd']['password'])

# MQTT client
mqttc = mqtt.Client(client_id=config['mqtt']['client_id'], protocol=3)
mqttc.on_message = on_mqtt_message
mqttc.on_disconnect = on_mqtt_disconnect
mqttc.on_connect = on_mqtt_connect

if (config['mqtt']['user'] != ''):
	mqttc.username_pw_set(config['mqtt']['user'], config['mqtt']['password'])

mqttc.connect(config['mqtt']['server'], config['mqtt']['port'], config['mqtt']['timeout'])

mqttc.loop_start()

while True:
	mpdc.ping()
	time.sleep(30)