Fixed tab and spaces mixing, spaces everywhere.

This commit is contained in:
Jonathan Schemoul
2017-04-11 15:52:49 +02:00
parent 586fb2ae62
commit 81758400b2
3 changed files with 231 additions and 231 deletions
+74 -74
View File
@@ -13,94 +13,94 @@ logging.basicConfig(level=logging.INFO)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def process_gateway_messages(gateway, client): def process_gateway_messages(gateway, client):
while True: while True:
try: try:
packet = gateway._queue.get() packet = gateway._queue.get()
_LOGGER.debug("data from queuee: " + format(packet)) _LOGGER.debug("data from queuee: " + format(packet))
sid = packet.get("sid", None) sid = packet.get("sid", None)
model = packet.get("model", "") model = packet.get("model", "")
data = packet.get("data", "") data = packet.get("data", "")
if (sid != None and data != ""): if (sid != None and data != ""):
data_decoded = json.loads(data) data_decoded = json.loads(data)
client.publish(model, sid, data_decoded) client.publish(model, sid, data_decoded)
gateway._queue.task_done() gateway._queue.task_done()
except Exception as e: except Exception as e:
_LOGGER.error('Error while sending from gateway to mqtt: ', str(e)) _LOGGER.error('Error while sending from gateway to mqtt: ', str(e))
def read_motion_data(gateway, client, polling_interval, polling_models): def read_motion_data(gateway, client, polling_interval, polling_models):
first = True first = True
while True: while True:
try: try:
for device_type in gateway.XIAOMI_DEVICES: for device_type in gateway.XIAOMI_DEVICES:
devices = gateway.XIAOMI_DEVICES[device_type] devices = gateway.XIAOMI_DEVICES[device_type]
for device in devices: for device in devices:
model = device.get("model", "") model = device.get("model", "")
if (model not in polling_models): if (model not in polling_models):
continue continue
sid = device['sid'] sid = device['sid']
sensor_resp = gateway.get_from_hub(sid) sensor_resp = gateway.get_from_hub(sid)
if (sensor_resp == None): if (sensor_resp == None):
continue; continue;
if (sensor_resp['sid'] != sid): if (sensor_resp['sid'] != sid):
_LOGGER.error("Error: Response sid(" + sensor_resp['sid'] + ") differs from requested(" + sid + "). Skipping.") _LOGGER.error("Error: Response sid(" + sensor_resp['sid'] + ") differs from requested(" + sid + "). Skipping.")
continue; continue;
data = json.loads(sensor_resp['data']) data = json.loads(sensor_resp['data'])
state = data.get("status", None) state = data.get("status", None)
short_id = sensor_resp['short_id'] short_id = sensor_resp['short_id']
if ( device['data'] != data or first): if ( device['data'] != data or first):
device['data'] = data device['data'] = data
_LOGGER.debug("Polling result differs for " + str(model) + " with sid(First: " + str(first) + "): " + str(sid) + "; " + str(data)) _LOGGER.debug("Polling result differs for " + str(model) + " with sid(First: " + str(first) + "): " + str(sid) + "; " + str(data))
client.publish(model, sid, data) client.publish(model, sid, data)
first = False first = False
except Exception as e: except Exception as e:
_LOGGER.error('Error while sending from mqtt to gateway: ', str(e)) _LOGGER.error('Error while sending from mqtt to gateway: ', str(e))
time.sleep(polling_interval) time.sleep(polling_interval)
def process_mqtt_messages(gateway, client): def process_mqtt_messages(gateway, client):
while True: while True:
try: try:
data = client._queue.get() data = client._queue.get()
_LOGGER.debug("data from mqtt: " + format(data)) _LOGGER.debug("data from mqtt: " + format(data))
sid = data.get("sid", None) sid = data.get("sid", None)
values = data.get("values", dict()) values = data.get("values", dict())
resp = gateway.write_to_hub(sid, **values) resp = gateway.write_to_hub(sid, **values)
client._queue.task_done() client._queue.task_done()
except Exception as e: except Exception as e:
_LOGGER.error('Error while sending from mqtt to gateway: ', str(e)) _LOGGER.error('Error while sending from mqtt to gateway: ', str(e))
if __name__ == "__main__": if __name__ == "__main__":
_LOGGER.info("Loading config file...") _LOGGER.info("Loading config file...")
config=yamlparser.load_yaml('config/config.yaml') config=yamlparser.load_yaml('config/config.yaml')
gateway_pass = yamlparser.get_gateway_password(config) gateway_pass = yamlparser.get_gateway_password(config)
polling_interval = config['gateway'].get("polling_interval", 2) polling_interval = config['gateway'].get("polling_interval", 2)
polling_models = config['gateway'].get("polling_models", ['motion']) polling_models = config['gateway'].get("polling_models", ['motion'])
_LOGGER.info("Init mqtt client.") _LOGGER.info("Init mqtt client.")
client = mqtt.Mqtt(config) client = mqtt.Mqtt(config)
client.connect() client.connect()
#only this devices can be controlled from MQTT #only this devices can be controlled from MQTT
client.subscribe("gateway", "+", "+", "set") client.subscribe("gateway", "+", "+", "set")
client.subscribe("gateway", "+", "write", None) client.subscribe("gateway", "+", "write", None)
client.subscribe("plug", "+", "status", "set") client.subscribe("plug", "+", "status", "set")
gateway = XiaomiHub(gateway_pass) gateway = XiaomiHub(gateway_pass)
t1 = threading.Thread(target=process_gateway_messages, args=[gateway, client]) t1 = threading.Thread(target=process_gateway_messages, args=[gateway, client])
t1.daemon = True t1.daemon = True
t1.start() t1.start()
t2 = threading.Thread(target=process_mqtt_messages, args=[gateway, client]) t2 = threading.Thread(target=process_mqtt_messages, args=[gateway, client])
t2.daemon = True t2.daemon = True
t2.start() t2.start()
t3 = threading.Thread(target=read_motion_data, args=[gateway, client, polling_interval, polling_models]) t3 = threading.Thread(target=read_motion_data, args=[gateway, client, polling_interval, polling_models])
t3.daemon = True t3.daemon = True
t3.start() t3.start()
while True: while True:
time.sleep(10) time.sleep(10)
+156 -156
View File
@@ -8,185 +8,185 @@ import json
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class Mqtt: class Mqtt:
username = "" username = ""
password = "" password = ""
server = "localhost" server = "localhost"
port = 1883 port = 1883
prefix = "home" prefix = "home"
_client = None _client = None
_sids = None _sids = None
_queue = None _queue = None
_threads = None _threads = None
def __init__(self, config): def __init__(self, config):
if (config == None): if (config == None):
raise "Config is null" raise "Config is null"
#load sids dictionary #load sids dictionary
self._sids = config.get("sids", None) self._sids = config.get("sids", None)
if (self._sids == None): if (self._sids == None):
self._sids = dict({}) self._sids = dict({})
#load mqtt settings #load mqtt settings
mqttConfig = config.get("mqtt", None) mqttConfig = config.get("mqtt", None)
if (mqttConfig == None): if (mqttConfig == None):
raise "Config mqtt section is null" raise "Config mqtt section is null"
self.username = mqttConfig.get("username", "") self.username = mqttConfig.get("username", "")
self.password = mqttConfig.get("password", "") self.password = mqttConfig.get("password", "")
self.server = mqttConfig.get("server", "localhost") self.server = mqttConfig.get("server", "localhost")
self.port = mqttConfig.get("port", 1883) self.port = mqttConfig.get("port", 1883)
self.prefix = mqttConfig.get("prefix", "home") self.prefix = mqttConfig.get("prefix", "home")
self._queue = Queue() self._queue = Queue()
self._threads = [] self._threads = []
def connect(self): def connect(self):
_LOGGER.info("Connecting to MQTT server " + self.server + ":" + str(self.port) + " with username (" + self.username + ":" + self.password + ")") _LOGGER.info("Connecting to MQTT server " + self.server + ":" + str(self.port) + " with username (" + self.username + ":" + self.password + ")")
self._client = mqtt.Client() self._client = mqtt.Client()
if (self.username != "" and self.password != ""): if (self.username != "" and self.password != ""):
self._client.username_pw_set(self.username, self.password) self._client.username_pw_set(self.username, self.password)
self._client.on_message = self._mqtt_process_message self._client.on_message = self._mqtt_process_message
self._client.on_connect = self._mqtt_on_connect self._client.on_connect = self._mqtt_on_connect
self._client.connect(self.server, self.port, 60) self._client.connect(self.server, self.port, 60)
#run message processing loop #run message processing loop
t1 = Thread(target=self._mqtt_loop) t1 = Thread(target=self._mqtt_loop)
t1.start() t1.start()
self._threads.append(t1) self._threads.append(t1)
def subscribe(self, model="+", name="+", prop="+", command=None): def subscribe(self, model="+", name="+", prop="+", command=None):
topic = self.prefix + "/" + model + "/" + name + "/" + prop topic = self.prefix + "/" + model + "/" + name + "/" + prop
if command is not None: if command is not None:
topic += "/" + command topic += "/" + command
_LOGGER.info("Subscibing to " + topic + ".") _LOGGER.info("Subscibing to " + topic + ".")
self._client.subscribe(topic) self._client.subscribe(topic)
def publish(self, model, sid, data, retain=True): def publish(self, model, sid, data, retain=True):
sidprops = self._sids.get(sid, None) sidprops = self._sids.get(sid, None)
if (sidprops != None): if (sidprops != None):
model = sidprops.get("model",model) model = sidprops.get("model",model)
sid = sidprops.get("name",sid) sid = sidprops.get("name",sid)
# _LOGGER.info("data is " + format(data)) # _LOGGER.info("data is " + format(data))
PATH_FMT = self.prefix + "/{model}/{sid}/{prop}" PATH_FMT = self.prefix + "/{model}/{sid}/{prop}"
for key, value in data.items(): for key, value in data.items():
# fix for latest motion value # fix for latest motion value
if (model == "motion" and key == "no_motion"): if (model == "motion" and key == "no_motion"):
key="status" key="status"
value="no_motion" value="no_motion"
if (model == "magnet" and key == "no_close"): if (model == "magnet" and key == "no_close"):
key="status" key="status"
value="open" value="open"
# do not retain event-based sensors (like switches and cubes). # do not retain event-based sensors (like switches and cubes).
if (model in ["switch", "cube"]): if (model in ["switch", "cube"]):
retain = False retain = False
# fix for rgb format # fix for rgb format
if (key == "rgb" and self._is_int(value)): if (key == "rgb" and self._is_int(value)):
value = self._color_xiaomi_to_rgb(value) value = self._color_xiaomi_to_rgb(value)
topic = PATH_FMT.format(model=model, sid=sid, prop=key) topic = PATH_FMT.format(model=model, sid=sid, prop=key)
_LOGGER.info("Publishing message to topic " + topic + ": " + str(value) + ".") _LOGGER.info("Publishing message to topic " + topic + ": " + str(value) + ".")
self._client.publish(topic, payload=value, qos=0, retain=retain) self._client.publish(topic, payload=value, qos=0, retain=retain)
def _mqtt_on_connect(self, client, userdata, rc, unk): def _mqtt_on_connect(self, client, userdata, rc, unk):
_LOGGER.info("Connected to mqtt server.") _LOGGER.info("Connected to mqtt server.")
def _mqtt_process_message(self, client, userdata, msg): def _mqtt_process_message(self, client, userdata, msg):
_LOGGER.info("Processing message in " + str(msg.topic) + ": " + str(msg.payload) + ".") _LOGGER.info("Processing message in " + str(msg.topic) + ": " + str(msg.payload) + ".")
parts = msg.topic.split("/") parts = msg.topic.split("/")
if len(parts) < 4: if len(parts) < 4:
# should we return an error message ? # should we return an error message ?
return return
model = parts[1] model = parts[1]
query_sid = parts[2] #sid or name part query_sid = parts[2] #sid or name part
param = parts[3] #param part param = parts[3] #param part
method = None method = None
if len(parts) > 4: if len(parts) > 4:
method = parts[4] method = parts[4]
else: else:
method = parts[3] method = parts[3]
name = "" # we will find it next name = "" # we will find it next
sid = query_sid sid = query_sid
isFound = False isFound = False
for current_sid in self._sids: for current_sid in self._sids:
if (current_sid == None): if (current_sid == None):
continue continue
sidprops = self._sids.get(current_sid, None) sidprops = self._sids.get(current_sid, None)
if sidprops == None: if sidprops == None:
continue continue
sidname = sidprops.get("name", current_sid) sidname = sidprops.get("name", current_sid)
sidmodel = sidprops.get("model", "") sidmodel = sidprops.get("model", "")
if (sidname == query_sid and sidmodel == model): if (sidname == query_sid and sidmodel == model):
sid = current_sid sid = current_sid
name = sidname name = sidname
isFound = True isFound = True
break break
else: else:
_LOGGER.debug(sidmodel + "-" + sidname + " is not " + model + "-" + query_sid + ".") _LOGGER.debug(sidmodel + "-" + sidname + " is not " + model + "-" + query_sid + ".")
continue continue
if isFound == False: if isFound == False:
# should we return an error message ? # should we return an error message ?
return return
if method == "set": if method == "set":
# use single value set method # use single value set method
value = (msg.payload).decode('utf-8') value = (msg.payload).decode('utf-8')
if self._is_int(value): if self._is_int(value):
value = int(value) value = int(value)
# fix for rgb format # fix for rgb format
if (param == "rgb" and "," in str(value)): if (param == "rgb" and "," in str(value)):
value = self._color_rgb_to_xiaomi(value) value = self._color_rgb_to_xiaomi(value)
# prepare values dict # prepare values dict
data = {'sid': sid, 'model': model, 'name': name, data = {'sid': sid, 'model': model, 'name': name,
'values': {param: value}} 'values': {param: value}}
# put in process queuee # put in process queuee
self._queue.put(data) self._queue.put(data)
elif method == "write": elif method == "write":
# use raw write method to the sensor, we expect a jsonified dict here. # use raw write method to the sensor, we expect a jsonified dict here.
values = json.loads((msg.payload).decode('utf-8')) values = json.loads((msg.payload).decode('utf-8'))
data = {'sid': sid, 'model': model, 'name': name, data = {'sid': sid, 'model': model, 'name': name,
'values': values} 'values': values}
# put in process queuee # put in process queuee
self._queue.put(data) self._queue.put(data)
def _mqtt_loop(self): def _mqtt_loop(self):
_LOGGER.info("Starting mqtt loop.") _LOGGER.info("Starting mqtt loop.")
self._client.loop_forever() self._client.loop_forever()
def _color_xiaomi_to_rgb(self, xiaomi_color): def _color_xiaomi_to_rgb(self, xiaomi_color):
intval = int(xiaomi_color) intval = int(xiaomi_color)
blue = (intval) & 255 blue = (intval) & 255
green = (intval >> 8) & 255 green = (intval >> 8) & 255
red = (intval >> 16) & 255 red = (intval >> 16) & 255
bright = (intval >> 24) & 255 bright = (intval >> 24) & 255
value = str(red)+","+str(green)+","+str(blue)+","+str(bright) value = str(red)+","+str(green)+","+str(blue)+","+str(bright)
return value return value
def _color_rgb_to_xiaomi(self, rgb_string): def _color_rgb_to_xiaomi(self, rgb_string):
arr = rgb_string.split(",") arr = rgb_string.split(",")
r = int(arr[0]) r = int(arr[0])
g = int(arr[1]) g = int(arr[1])
b = int(arr[2]) b = int(arr[2])
if len(arr)>3: if len(arr)>3:
bright = int(arr[3]) bright = int(arr[3])
else: else:
bright = 255 bright = 255
value = int('%02x%02x%02x%02x' % (bright, r, g, b), 16) value = int('%02x%02x%02x%02x' % (bright, r, g, b), 16)
return value return value
def _is_int(self, x): def _is_int(self, x):
try: try:
tmp = int(x) tmp = int(x)
return True return True
except Exception as e: except Exception as e:
return False return False
+1 -1
View File
@@ -55,7 +55,7 @@ class XiaomiHub:
_LOGGER.error("Cannot discover hub using whois: {0}".format(e)) _LOGGER.error("Cannot discover hub using whois: {0}".format(e))
self._socket.close() self._socket.close()
if self.GATEWAY_IP is None: if self.GATEWAY_IP is None:
_LOGGER.error('No Gateway found. Cannot continue') _LOGGER.error('No Gateway found. Cannot continue')
return None return None