Skip to content

Instantly share code, notes, and snippets.

@mvillarejo
Created September 21, 2017 14:20
Show Gist options
  • Save mvillarejo/588bb4f3eec94e9e09cf48f7e82e145a to your computer and use it in GitHub Desktop.
Save mvillarejo/588bb4f3eec94e9e09cf48f7e82e145a to your computer and use it in GitHub Desktop.
smartthings_to_sensor.py
"""
Smartthings to sensor MQTT listener
Subscribe for specific smartthings topic and dinamically create all the sensors
from SmartThings hub following a naming convention (case insensitive):
* BUTTON: Button xxxxx
* Door Sensors: DS xxxxx
* Motion Sensor: MS xxxxx
* Temperature and Humidity Sensor: THS
smartthings_to_sensor:
topic: 'smartthings/+/+'
"""
import asyncio
import logging
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.components import mqtt
import homeassistant.helpers.config_validation as cv
DOMAIN = "smartthings_to_sensor"
_LOGGER = logging.getLogger(__name__)
# List of component names (string) your component depends upon.
DEPENDENCIES = ['mqtt']
CONF_TOPIC = 'topic'
DEFAULT_TOPIC = 'smartthings/+/+'
BUTTON_SENSOR_PREFIX = 'BUTTON'
DOOR_SENSOR_PREFIX = 'DS'
MOTION_SENSOR_PREFIX = 'MS'
TEMP_HUM_SENSOR_PREFIX = 'THS'
# List of smatthings attributes
ATTR_BUTTON = 'button'
ATTR_BATTERY = 'battery'
ATTR_CONTACT = 'contact'
ATTR_MOTION = 'motion'
ATTR_TEMPERATURE = 'temperature'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(CONF_TOPIC, default=DEFAULT_TOPIC): mqtt.valid_subscribe_topic
})
}, extra=vol.ALLOW_EXTRA)
@asyncio.coroutine
def async_setup(hass, config):
"""Setup the MQTT example component."""
topic = config[DOMAIN][CONF_TOPIC]
_LOGGER.info("Setting up %s topic: %s", DOMAIN, topic)
# entity_id = 'mqtt_example.last_message'
@callback
def message_received(topic, payload, qos):
"""A new MQTT message has been received."""
# hass.states.async_set(entity_id, payload)
topic_list = topic.split('/')
topic_sensor_name = topic_list[1].replace(' ', '_').lower()
attribute = topic_list[2]
_LOGGER.info("topic_sensor_name: %s attribute: %s", topic_sensor_name, attribute)
sensor_name = "sensor.{}".format(topic_sensor_name)
_LOGGER.info("Sensor_name: %s", sensor_name)
entity = hass.states.get(sensor_name)
if not entity:
# hass.states.set(topic_sensor_name, "n/a", {})
hass.states.async_set(sensor_name, "n/a", {})
entity = hass.states.get(sensor_name)
state = entity.as_dict().get('state')
attributes = entity.as_dict().get("attributes")
# Add sensor name as friendly_name
attributes['friendly_name'] = topic_list[1]
# main atribute became state
if DOOR_SENSOR_PREFIX.lower() in sensor_name:
# TODO: use another icons for doors/windows
# Cancela Icon (3 bars)
attributes['icon'] = "mdi:view-parallel"
if attribute == ATTR_CONTACT:
state = payload
else:
attributes[attribute] = payload
elif MOTION_SENSOR_PREFIX.lower() in sensor_name:
if attribute == ATTR_MOTION:
state = payload
else:
attributes[attribute] = payload
elif TEMP_HUM_SENSOR_PREFIX.lower() in sensor_name:
if attribute == ATTR_TEMPERATURE:
state = payload
else:
attributes[attribute] = payload
elif BUTTON_SENSOR_PREFIX.lower() in sensor_name:
if attribute == ATTR_BUTTON:
state = payload
else:
attributes[attribute] = payload
else:
_LOGGER.warning("Unknown sensor: %s attribute: %s, payload: %s", sensor_name,
attribute, payload)
_LOGGER.info("sensor: %s attribute: %s, payload: %s", sensor_name,
attribute, payload)
hass.states.async_set(sensor_name, state, attributes)
_LOGGER.info("current attributes: %s (%s)", attributes, entity.last_updated)
# last_changed: last time the state was changed, not the attributes.
# last_updated: last time this object was updated.
yield from mqtt.async_subscribe(hass, topic, message_received)
# Service to publish a message on MQTT.
@callback
def set_state_service(call):
"""Service to send a message."""
mqtt.async_publish(hass, topic, call.data.get('new_state'))
_LOGGER.info("Setting up %s topic: %s", DOMAIN, CONF_TOPIC)
# Register our service with Home Assistant.
hass.services.async_register(DOMAIN, 'set_state', set_state_service)
# Return boolean to indicate that initialization was successfully.
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment