2019-02-13 21:21:14 +01:00
|
|
|
"""Support for submitting data to Thingspeak."""
|
2016-10-27 08:56:51 +02:00
|
|
|
import logging
|
|
|
|
|
2016-10-27 09:37:02 +02:00
|
|
|
from requests.exceptions import RequestException
|
2019-10-13 14:56:02 +02:00
|
|
|
import thingspeak
|
2016-10-30 09:58:34 +01:00
|
|
|
import voluptuous as vol
|
2016-10-27 08:56:51 +02:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-07-31 21:25:30 +02:00
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_ID,
|
|
|
|
CONF_WHITELIST,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
STATE_UNKNOWN,
|
|
|
|
)
|
2018-07-18 11:54:27 +02:00
|
|
|
from homeassistant.helpers import event, state as state_helper
|
2016-10-27 08:56:51 +02:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
DOMAIN = "thingspeak"
|
2016-10-30 09:58:34 +01:00
|
|
|
|
2016-10-27 08:56:51 +02:00
|
|
|
TIMEOUT = 5
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
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,
|
|
|
|
)
|
2016-10-27 08:56:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-10-30 09:58:34 +01:00
|
|
|
"""Set up the Thingspeak environment."""
|
2016-10-27 08:56:51 +02:00
|
|
|
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
api_key = conf.get(CONF_API_KEY)
|
|
|
|
channel_id = conf.get(CONF_ID)
|
|
|
|
entity = conf.get(CONF_WHITELIST)
|
|
|
|
|
|
|
|
try:
|
2019-07-31 21:25:30 +02:00
|
|
|
channel = thingspeak.Channel(channel_id, write_key=api_key, timeout=TIMEOUT)
|
2016-10-27 08:56:51 +02:00
|
|
|
channel.get()
|
2016-10-27 09:37:02 +02:00
|
|
|
except RequestException:
|
2019-07-31 21:25:30 +02:00
|
|
|
_LOGGER.error(
|
|
|
|
"Error while accessing the ThingSpeak channel. "
|
|
|
|
"Please check that the channel exists and your "
|
|
|
|
"API key is correct"
|
|
|
|
)
|
2016-10-27 08:56:51 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def thingspeak_listener(entity_id, old_state, new_state):
|
2017-02-13 11:25:28 +01:00
|
|
|
"""Listen for new events and send them to Thingspeak."""
|
2016-10-27 08:56:51 +02:00
|
|
|
if new_state is None or new_state.state in (
|
2019-07-31 21:25:30 +02:00
|
|
|
STATE_UNKNOWN,
|
|
|
|
"",
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
):
|
2016-10-27 08:56:51 +02:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
if new_state.entity_id != entity:
|
|
|
|
return
|
|
|
|
_state = state_helper.state_as_number(new_state)
|
|
|
|
except ValueError:
|
|
|
|
return
|
|
|
|
try:
|
2019-07-31 21:25:30 +02:00
|
|
|
channel.update({"field1": _state})
|
2016-10-27 09:37:02 +02:00
|
|
|
except RequestException:
|
2019-07-31 21:25:30 +02:00
|
|
|
_LOGGER.error("Error while sending value '%s' to Thingspeak", _state)
|
2016-10-27 08:56:51 +02:00
|
|
|
|
|
|
|
event.track_state_change(hass, entity, thingspeak_listener)
|
|
|
|
|
|
|
|
return True
|