1 """Support for submitting data to Thingspeak."""
5 from requests.exceptions
import RequestException
7 import voluptuous
as vol
21 _LOGGER = logging.getLogger(__name__)
27 CONFIG_SCHEMA = vol.Schema(
31 vol.Required(CONF_API_KEY): cv.string,
32 vol.Required(CONF_ID): int,
33 vol.Required(CONF_WHITELIST): cv.string,
37 extra=vol.ALLOW_EXTRA,
41 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
42 """Set up the Thingspeak environment."""
44 api_key = conf.get(CONF_API_KEY)
45 channel_id = conf.get(CONF_ID)
46 entity = conf.get(CONF_WHITELIST)
49 channel = thingspeak.Channel(channel_id, api_key=api_key, timeout=TIMEOUT)
51 except RequestException:
53 "Error while accessing the ThingSpeak channel. "
54 "Please check that the channel exists and your API key is correct"
58 def thingspeak_listener(entity_id, old_state, new_state):
59 """Listen for new events and send them to Thingspeak."""
60 if new_state
is None or new_state.state
in (
67 if new_state.entity_id != entity:
69 _state = state_helper.state_as_number(new_state)
73 channel.update({
"field1": _state})
74 except RequestException:
75 _LOGGER.error(
"Error while sending value '%s' to Thingspeak", _state)
77 event.track_state_change(hass, entity, thingspeak_listener)
bool setup(HomeAssistant hass, ConfigType config)