#!/usr/bin/python

import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import cgate

HOSTNAME='192.168.1.254'
MQTT_HOSTNAME='192.168.1.254'

# Connect to cgate
cg_mon = cgate.CGateMonitor(HOSTNAME)
cg_mon.connect()

print "Listening for events"
while True:
    (id, state) = cg_mon.get_next()
    print id, " ",  state
    publish.single('lights/%s/state' % id,
                   state, hostname=MQTT_HOSTNAME,
                   protocol=mqtt.MQTTv31, retain=True)
