diff --git a/.coveragerc b/.coveragerc index 32cbdc38047..67b347c5ee1 100644 --- a/.coveragerc +++ b/.coveragerc @@ -14,6 +14,9 @@ omit = homeassistant/components/bloomsky.py homeassistant/components/*/bloomsky.py + homeassistant/components/dweet.py + homeassistant/components/*/dweet.py + homeassistant/components/ecobee.py homeassistant/components/*/ecobee.py @@ -143,7 +146,6 @@ omit = homeassistant/components/sensor/cpuspeed.py homeassistant/components/sensor/deutsche_bahn.py homeassistant/components/sensor/dht.py - homeassistant/components/sensor/dweet.py homeassistant/components/sensor/efergy.py homeassistant/components/sensor/eliqonline.py homeassistant/components/sensor/forecast.py diff --git a/homeassistant/components/dweet.py b/homeassistant/components/dweet.py new file mode 100644 index 00000000000..49c1e74f232 --- /dev/null +++ b/homeassistant/components/dweet.py @@ -0,0 +1,73 @@ +""" +A component which allows you to send data to Dweet.io. + +For more details about this component, please refer to the documentation at +https://home-assistant.io/components/dweet/ +""" +import logging +from datetime import timedelta +import voluptuous as vol + +from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNKNOWN +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers import state as state_helper +from homeassistant.util import Throttle + + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = "dweet" +DEPENDENCIES = [] + +REQUIREMENTS = ['dweepy==0.2.0'] + +CONF_NAME = 'name' +CONF_WHITELIST = 'whitelist' + +MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1) + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + vol.Required(CONF_NAME): cv.string, + vol.Required(CONF_WHITELIST): cv.string, + }), +}, extra=vol.ALLOW_EXTRA) + + +# pylint: disable=too-many-locals +def setup(hass, config): + """Setup the Dweet.io component.""" + conf = config[DOMAIN] + name = conf[CONF_NAME] + whitelist = conf.get(CONF_WHITELIST, []) + json_body = {} + + def dweet_event_listener(event): + """Listen for new messages on the bus and sends them to Dweet.io.""" + state = event.data.get('new_state') + if state is None or state.state in (STATE_UNKNOWN, '') \ + or state.entity_id not in whitelist: + return + + try: + _state = state_helper.state_as_number(state) + except ValueError: + _state = state.state + + json_body[state.attributes.get('friendly_name')] = _state + + send_data(name, json_body) + + hass.bus.listen(EVENT_STATE_CHANGED, dweet_event_listener) + + return True + + +@Throttle(MIN_TIME_BETWEEN_UPDATES) +def send_data(name, msg): + """Send the collected data to Dweet.io.""" + import dweepy + try: + dweepy.dweet_for(name, msg) + except dweepy.DweepyError: + _LOGGER.error("Error saving data '%s' to Dweet.io", msg) diff --git a/requirements_all.txt b/requirements_all.txt index 082166b5a99..2b876af7448 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -40,6 +40,7 @@ blockchain==1.3.1 # homeassistant.components.notify.xmpp dnspython3==1.12.0 +# homeassistant.components.dweet # homeassistant.components.sensor.dweet dweepy==0.2.0