core/homeassistant/components/dweet/sensor.py

117 lines
3.2 KiB
Python
Raw Normal View History

"""Support for showing values from Dweet.io."""
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 (
2019-07-31 19:25:30 +00:00
CONF_NAME,
CONF_VALUE_TEMPLATE,
CONF_UNIT_OF_MEASUREMENT,
CONF_DEVICE,
)
2015-12-10 10:11:35 +00:00
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Dweet.io Sensor"
SCAN_INTERVAL = timedelta(minutes=1)
2019-07-31 19:25:30 +00:00
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
def setup_platform(hass, config, add_entities, 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:
2019-07-31 19:25:30 +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
2015-12-10 10:11:35 +00:00
2019-07-31 19:25:30 +00:00
if value_template.render_with_possible_json_value(content) == "":
_LOGGER.error("%s was not found", value_template)
return
2015-12-12 20:56:47 +00:00
2015-12-10 10:11:35 +00:00
dweet = DweetData(device)
add_entities([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
self._state = None
2015-12-10 10:11:35 +00:00
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 = None
2017-05-11 20:48:03 +00:00
else:
2019-07-31 19:25:30 +00:00
values = json.dumps(self.dweet.data[0]["content"])
2017-05-11 20:48:03 +00:00
self._state = self._value_template.render_with_possible_json_value(
2019-07-31 19:25:30 +00:00
values, None
)
2017-05-11 20:48:03 +00:00
2015-12-10 10:11:35 +00:00
class DweetData:
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