From d9999f36e85bbe915e5a86de038ce2e378f41adb Mon Sep 17 00:00:00 2001 From: Simon Szustkowski Date: Thu, 27 Oct 2016 08:56:51 +0200 Subject: [PATCH] Added a ThingSpeak component (#4027) * Added a ThingSpeak component * Forgot a colon. Fixed it * Some config variables are better required * New requirements created by the script * Updated the .coveragerc * Fixed small linting errors * Removed unneccessary validation * Even more linting error fixes * Changed the way the component listens to state changes * Removed unneccessary declaration of 'state' variable, referring to new_state instead --- .coveragerc | 1 + homeassistant/components/thingspeak.py | 70 ++++++++++++++++++++++++++ requirements_all.txt | 3 ++ 3 files changed, 74 insertions(+) create mode 100644 homeassistant/components/thingspeak.py diff --git a/.coveragerc b/.coveragerc index df71e507c28..03e145a74de 100644 --- a/.coveragerc +++ b/.coveragerc @@ -307,6 +307,7 @@ omit = homeassistant/components/switch/tplink.py homeassistant/components/switch/transmission.py homeassistant/components/switch/wake_on_lan.py + homeassistant/components/thingspeak.py homeassistant/components/upnp.py homeassistant/components/weather/openweathermap.py homeassistant/components/zeroconf.py diff --git a/homeassistant/components/thingspeak.py b/homeassistant/components/thingspeak.py new file mode 100644 index 00000000000..f1689c1833e --- /dev/null +++ b/homeassistant/components/thingspeak.py @@ -0,0 +1,70 @@ +"""A component to submit data to thingspeak.""" +import logging + +import voluptuous as vol + +from homeassistant.const import ( + CONF_API_KEY, CONF_ID, CONF_WHITELIST, + STATE_UNAVAILABLE, STATE_UNKNOWN) +from homeassistant.helpers import state as state_helper +import homeassistant.helpers.config_validation as cv +import homeassistant.helpers.event as event + +REQUIREMENTS = ['thingspeak==0.4.0'] + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = 'thingspeak' +TIMEOUT = 5 + +# Validate the config +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + vol.Required(CONF_API_KEY): cv.string, + vol.Required(CONF_ID): int, + vol.Required(CONF_WHITELIST): cv.string + }), + }, extra=vol.ALLOW_EXTRA) + + +def setup(hass, config): + """Setup the thingspeak environment.""" + import thingspeak + + # Read out config values + conf = config[DOMAIN] + api_key = conf.get(CONF_API_KEY) + channel_id = conf.get(CONF_ID) + entity = conf.get(CONF_WHITELIST) + + try: + channel = thingspeak.Channel( + channel_id, api_key=api_key, timeout=TIMEOUT) + channel.get() + except: + _LOGGER.error("Error while accessing the ThingSpeak channel. " + "Please check that the channel exists and your " + "API key is correct.") + return False + + def thingspeak_listener(entity_id, old_state, new_state): + """Listen for new events and send them to thingspeak.""" + if new_state is None or new_state.state in ( + STATE_UNKNOWN, '', STATE_UNAVAILABLE): + return + try: + if new_state.entity_id != entity: + return + _state = state_helper.state_as_number(new_state) + except ValueError: + return + try: + channel.update({'field1': _state}) + except: + _LOGGER.error( + 'Error while sending value "%s" to Thingspeak', + _state) + + event.track_state_change(hass, entity, thingspeak_listener) + + return True diff --git a/requirements_all.txt b/requirements_all.txt index 6233415c08d..818ff621b0f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -495,6 +495,9 @@ tellive-py==0.5.2 # homeassistant.components.sensor.temper temperusb==1.5.1 +# homeassistant.components.thingspeak +thingspeak==0.4.0 + # homeassistant.components.sensor.transmission # homeassistant.components.switch.transmission transmissionrpc==0.11