2016-10-30 08:58:34 +00:00
|
|
|
"""
|
|
|
|
A component to submit data to thingspeak.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/thingspeak/
|
|
|
|
"""
|
2016-10-27 06:56:51 +00:00
|
|
|
import logging
|
|
|
|
|
2016-10-27 07:37:02 +00:00
|
|
|
from requests.exceptions import RequestException
|
2016-10-30 08:58:34 +00:00
|
|
|
import voluptuous as vol
|
2016-10-27 06:56:51 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2016-10-30 08:58:34 +00:00
|
|
|
CONF_API_KEY, CONF_ID, CONF_WHITELIST, STATE_UNAVAILABLE, STATE_UNKNOWN)
|
2016-10-27 06:56:51 +00:00
|
|
|
from homeassistant.helpers import state as state_helper
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
import homeassistant.helpers.event as event
|
|
|
|
|
2017-02-13 10:25:28 +00:00
|
|
|
REQUIREMENTS = ['thingspeak==0.4.1']
|
2016-10-27 06:56:51 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'thingspeak'
|
2016-10-30 08:58:34 +00:00
|
|
|
|
2016-10-27 06:56:51 +00:00
|
|
|
TIMEOUT = 5
|
|
|
|
|
|
|
|
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
|
|
|
|
}),
|
2016-10-30 08:58:34 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2016-10-27 06:56:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-10-30 08:58:34 +00:00
|
|
|
"""Set up the Thingspeak environment."""
|
2016-10-27 06:56:51 +00:00
|
|
|
import thingspeak
|
|
|
|
|
|
|
|
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(
|
2017-02-13 10:25:28 +00:00
|
|
|
channel_id, write_key=api_key, timeout=TIMEOUT)
|
2016-10-27 06:56:51 +00:00
|
|
|
channel.get()
|
2016-10-27 07:37:02 +00:00
|
|
|
except RequestException:
|
2016-10-27 06:56:51 +00:00
|
|
|
_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):
|
2017-02-13 10:25:28 +00:00
|
|
|
"""Listen for new events and send them to Thingspeak."""
|
2016-10-27 06:56:51 +00:00
|
|
|
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})
|
2016-10-27 07:37:02 +00:00
|
|
|
except RequestException:
|
2017-02-13 10:25:28 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Error while sending value '%s' to Thingspeak", _state)
|
2016-10-27 06:56:51 +00:00
|
|
|
|
|
|
|
event.track_state_change(hass, entity, thingspeak_listener)
|
|
|
|
|
|
|
|
return True
|