core/homeassistant/components/sensor/dweet.py

119 lines
3.6 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
2016-02-19 05:27:50 +00:00
from homeassistant.const import CONF_VALUE_TEMPLATE, STATE_UNKNOWN
2015-12-10 10:11:35 +00:00
from homeassistant.helpers.entity import Entity
2016-02-23 20:06:50 +00:00
from homeassistant.helpers import template
from homeassistant.util import Throttle
2015-12-10 10:11:35 +00:00
_LOGGER = logging.getLogger(__name__)
2015-12-10 10:13:59 +00:00
REQUIREMENTS = ['dweepy==0.2.0']
2015-12-10 10:11:35 +00:00
DEFAULT_NAME = 'Dweet.io Sensor'
CONF_DEVICE = 'device'
2016-02-23 05:21:49 +00:00
# Return cached results if last scan was less then this time ago.
2015-12-10 10:11:35 +00:00
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
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):
2016-02-23 05:21:49 +00:00
"""Setup the Dweet sensor."""
2015-12-10 10:11:35 +00:00
import dweepy
device = config.get('device')
2015-12-12 20:56:47 +00:00
value_template = config.get(CONF_VALUE_TEMPLATE)
2015-12-10 10:11:35 +00:00
2015-12-12 20:56:47 +00:00
if None in (device, value_template):
2015-12-10 10:11:35 +00:00
_LOGGER.error('Not all required config keys present: %s',
2015-12-12 20:56:47 +00:00
', '.join(CONF_DEVICE, CONF_VALUE_TEMPLATE))
2015-12-10 10:11:35 +00:00
return False
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)
return False
2015-12-12 20:56:47 +00:00
if template.render_with_possible_json_value(hass,
value_template,
content) is '':
_LOGGER.error("'%s' was not found", value_template)
return False
2015-12-10 10:11:35 +00:00
dweet = DweetData(device)
2015-12-12 20:56:47 +00:00
add_devices([DweetSensor(hass,
dweet,
2015-12-10 10:11:35 +00:00
config.get('name', DEFAULT_NAME),
2015-12-12 20:56:47 +00:00
value_template,
config.get('unit_of_measurement'))])
2015-12-10 10:11:35 +00:00
2015-12-12 20:56:47 +00:00
# pylint: disable=too-many-arguments
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
self.update()
@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."""
2015-12-13 00:00:12 +00:00
if self.dweet.data is None:
return STATE_UNKNOWN
else:
values = json.dumps(self.dweet.data[0]['content'])
2015-12-12 20:56:47 +00:00
value = template.render_with_possible_json_value(
self.hass, self._value_template, values)
2015-12-10 10:11:35 +00:00
return value
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()
# pylint: disable=too-few-public-methods
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
@Throttle(MIN_TIME_BETWEEN_UPDATES)
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:
_LOGGER.error("Device '%s' could not be found", self._device)
self.data = None