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
This commit is contained in:
parent
235e1a0885
commit
d9999f36e8
3 changed files with 74 additions and 0 deletions
|
@ -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
|
||||
|
|
70
homeassistant/components/thingspeak.py
Normal file
70
homeassistant/components/thingspeak.py
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue