core/homeassistant/components/sensor/dweet.py

120 lines
3.4 KiB
Python
Raw Normal View History

2015-12-10 10:11:35 +00:00
"""
2016-02-23 05:21:49 +00:00
Support for showing values from Dweet.io.
2015-12-10 10:11:35 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.dweet/
"""
2015-12-12 20:56:47 +00:00
import json
2016-02-19 05:27:50 +00:00
import logging
from datetime import timedelta
2015-12-10 10:11:35 +00:00
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME, CONF_VALUE_TEMPLATE, STATE_UNKNOWN, CONF_UNIT_OF_MEASUREMENT)
2015-12-10 10:11:35 +00:00
from homeassistant.helpers.entity import Entity
2017-05-11 20:48:03 +00:00
REQUIREMENTS = ['dweepy==0.3.0']
2015-12-10 10:11:35 +00:00
_LOGGER = logging.getLogger(__name__)
2015-12-10 10:11:35 +00:00
CONF_DEVICE = 'device'
DEFAULT_NAME = 'Dweet.io Sensor'
SCAN_INTERVAL = timedelta(minutes=1)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DEVICE): cv.string,
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
})
2015-12-10 10:11:35 +00:00
2015-12-12 20:56:47 +00:00
# pylint: disable=unused-variable, too-many-function-args
2015-12-10 10:11:35 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Dweet sensor."""
2015-12-10 10:11:35 +00:00
import dweepy
name = config.get(CONF_NAME)
device = config.get(CONF_DEVICE)
2015-12-12 20:56:47 +00:00
value_template = config.get(CONF_VALUE_TEMPLATE)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
2017-05-11 20:48:03 +00:00
if value_template is not None:
value_template.hass = hass
2015-12-10 10:11:35 +00:00
try:
2015-12-12 20:56:47 +00:00
content = json.dumps(dweepy.get_latest_dweet_for(device)[0]['content'])
2015-12-10 10:11:35 +00:00
except dweepy.DweepyError:
_LOGGER.error("Device/thing %s could not be found", device)
2015-12-10 10:11:35 +00:00
return False
if value_template.render_with_possible_json_value(content) == '':
_LOGGER.error("%s was not found", value_template)
2015-12-12 20:56:47 +00:00
return False
2015-12-10 10:11:35 +00:00
dweet = DweetData(device)
2017-05-11 20:48:03 +00:00
add_devices([DweetSensor(hass, dweet, name, value_template, unit)], True)
2015-12-10 10:11:35 +00:00
class DweetSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of a Dweet sensor."""
2015-12-12 20:56:47 +00:00
def __init__(self, hass, dweet, name, value_template, unit_of_measurement):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2015-12-12 20:56:47 +00:00
self.hass = hass
2015-12-10 10:11:35 +00:00
self.dweet = dweet
self._name = name
2015-12-12 20:56:47 +00:00
self._value_template = value_template
2015-12-10 10:11:35 +00:00
self._state = STATE_UNKNOWN
self._unit_of_measurement = unit_of_measurement
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
2015-12-10 10:11:35 +00:00
return self._name
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit the value is expressed in."""
2015-12-10 10:11:35 +00:00
return self._unit_of_measurement
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state."""
2017-05-11 20:48:03 +00:00
return self._state
2015-12-10 10:11:35 +00:00
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest data from REST API."""
2015-12-10 10:11:35 +00:00
self.dweet.update()
2017-05-11 20:48:03 +00:00
if self.dweet.data is None:
self._state = STATE_UNKNOWN
else:
values = json.dumps(self.dweet.data[0]['content'])
self._state = self._value_template.render_with_possible_json_value(
values, STATE_UNKNOWN)
2015-12-10 10:11:35 +00:00
class DweetData(object):
2016-03-08 15:46:34 +00:00
"""The class for handling the data retrieval."""
2015-12-10 10:11:35 +00:00
def __init__(self, device):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2015-12-10 10:11:35 +00:00
self._device = device
2015-12-13 00:00:12 +00:00
self.data = None
2015-12-10 10:11:35 +00:00
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest data from Dweet.io."""
2015-12-10 10:11:35 +00:00
import dweepy
try:
self.data = dweepy.get_latest_dweet_for(self._device)
except dweepy.DweepyError:
2017-05-11 20:48:03 +00:00
_LOGGER.warning("Device %s doesn't contain any data", self._device)
2015-12-10 10:11:35 +00:00
self.data = None