From 844c8149d774705b020a89371cd3c06006f28bdc Mon Sep 17 00:00:00 2001
From: Fabian Affolter <mail@fabian-affolter.ch>
Date: Sat, 17 Jun 2017 12:34:12 +0200
Subject: [PATCH] Add initial support for Shiftr.io (#7974)

* Add initial support for Shiftr.io

* Fix lint issue

* Use paho-mqtt instead of internal MQTT object

* remove async flavor while paho is not async
---
 .coveragerc                        |  1 +
 homeassistant/components/shiftr.py | 77 ++++++++++++++++++++++++++++++
 requirements_all.txt               |  1 +
 requirements_test_all.txt          |  1 +
 4 files changed, 80 insertions(+)
 create mode 100644 homeassistant/components/shiftr.py

diff --git a/.coveragerc b/.coveragerc
index 0b4306633e9..9ec3d706990 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -477,6 +477,7 @@ omit =
     homeassistant/components/sensor/xbox_live.py
     homeassistant/components/sensor/yweather.py
     homeassistant/components/sensor/zamg.py
+    homeassistant/components/shiftr.py
     homeassistant/components/spc.py
     homeassistant/components/switch/acer_projector.py
     homeassistant/components/switch/anel_pwrctrl.py
diff --git a/homeassistant/components/shiftr.py b/homeassistant/components/shiftr.py
new file mode 100644
index 00000000000..42c455acd49
--- /dev/null
+++ b/homeassistant/components/shiftr.py
@@ -0,0 +1,77 @@
+"""
+Support for Shiftr.io.
+
+For more details about this component, please refer to the documentation at
+https://home-assistant.io/components/shiftr/
+"""
+import logging
+
+import voluptuous as vol
+
+import homeassistant.helpers.config_validation as cv
+from homeassistant.const import (
+    CONF_PASSWORD, CONF_USERNAME, EVENT_STATE_CHANGED,
+    EVENT_HOMEASSISTANT_STOP)
+from homeassistant.helpers import state as state_helper
+
+REQUIREMENTS = ['paho-mqtt==1.2.3']
+
+_LOGGER = logging.getLogger(__name__)
+
+DOMAIN = 'shiftr'
+
+SHIFTR_BROKER = 'broker.shiftr.io'
+
+CONFIG_SCHEMA = vol.Schema({
+    DOMAIN: vol.Schema({
+        vol.Required(CONF_USERNAME): cv.string,
+        vol.Required(CONF_PASSWORD): cv.string,
+    }),
+}, extra=vol.ALLOW_EXTRA)
+
+
+def setup(hass, config):
+    """Initialize the Shiftr.io MQTT consumer."""
+    import paho.mqtt.client as mqtt
+    conf = config[DOMAIN]
+    username = conf.get(CONF_USERNAME)
+    password = conf.get(CONF_PASSWORD)
+
+    client_id = 'HomeAssistant'
+    port = 1883
+    keepalive = 600
+
+    mqttc = mqtt.Client(client_id, protocol=mqtt.MQTTv311)
+    mqttc.username_pw_set(username, password=password)
+    mqttc.connect(SHIFTR_BROKER, port=port, keepalive=keepalive)
+
+    def stop_shiftr(event):
+        """Stop the Shiftr.io MQTT component."""
+        mqttc.disconnect()
+
+    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_shiftr)
+
+    def shiftr_event_listener(event):
+        """Listen for new messages on the bus and sends them to Shiftr.io."""
+        state = event.data.get('new_state')
+        topic = state.entity_id.replace('.', '/')
+
+        try:
+            _state = state_helper.state_as_number(state)
+        except ValueError:
+            _state = state.state
+
+        try:
+            mqttc.publish(topic, _state, qos=0, retain=False)
+
+            if state.attributes:
+                for attribute, data in state.attributes.items():
+                    mqttc.publish(
+                        '/{}/{}'.format(topic, attribute), str(data), qos=0,
+                        retain=False)
+        except RuntimeError:
+            pass
+
+    hass.bus.listen(EVENT_STATE_CHANGED, shiftr_event_listener)
+
+    return True
diff --git a/requirements_all.txt b/requirements_all.txt
index 2aeb6f1d42f..77719422c0b 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -419,6 +419,7 @@ openhomedevice==0.4.2
 orvibo==1.1.1
 
 # homeassistant.components.mqtt
+# homeassistant.components.shiftr
 paho-mqtt==1.2.3
 
 # homeassistant.components.media_player.panasonic_viera
diff --git a/requirements_test_all.txt b/requirements_test_all.txt
index 49b6f2ae2f5..d1b226eda3e 100644
--- a/requirements_test_all.txt
+++ b/requirements_test_all.txt
@@ -72,6 +72,7 @@ libsoundtouch==0.3.0
 mficlient==0.3.0
 
 # homeassistant.components.mqtt
+# homeassistant.components.shiftr
 paho-mqtt==1.2.3
 
 # homeassistant.components.device_tracker.aruba
-- 
GitLab