Pulled from dev to get up-to-date
This commit is contained in:
commit
cceb79a378
34 files changed
+1194
-176
No files matched your search
@@ -11,9 +11,11 @@ import logging
|
||||
|
||||
import requests
|
||||
|
||||
from homeassistant.const import DEVICE_DEFAULT_NAME
|
||||
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, \
|
||||
DEVICE_DEFAULT_NAME
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.util import template, Throttle
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -50,36 +52,50 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
arest = ArestData(resource)
|
||||
|
||||
def make_renderer(value_template):
|
||||
""" Creates renderer based on variable_template value """
|
||||
if value_template is None:
|
||||
return lambda value: value
|
||||
|
||||
def _render(value):
|
||||
try:
|
||||
return template.render(hass, value_template, {'value': value})
|
||||
except TemplateError:
|
||||
_LOGGER.exception('Error parsing value')
|
||||
return value
|
||||
|
||||
return _render
|
||||
|
||||
dev = []
|
||||
|
||||
if var_conf is not None:
|
||||
for variable in config['monitored_variables']:
|
||||
for variable in var_conf:
|
||||
if variable['name'] not in response['variables']:
|
||||
_LOGGER.error('Variable: "%s" does not exist',
|
||||
variable['name'])
|
||||
continue
|
||||
|
||||
renderer = make_renderer(variable.get(CONF_VALUE_TEMPLATE))
|
||||
dev.append(ArestSensor(arest,
|
||||
resource,
|
||||
config.get('name', response['name']),
|
||||
variable['name'],
|
||||
variable=variable['name'],
|
||||
unit_of_measurement=variable.get(
|
||||
'unit_of_measurement')))
|
||||
ATTR_UNIT_OF_MEASUREMENT),
|
||||
renderer=renderer))
|
||||
|
||||
if pins is not None:
|
||||
for pinnum, pin in pins.items():
|
||||
renderer = make_renderer(pin.get(CONF_VALUE_TEMPLATE))
|
||||
dev.append(ArestSensor(ArestData(resource, pinnum),
|
||||
resource,
|
||||
config.get('name', response['name']),
|
||||
pin.get('name'),
|
||||
pin=pinnum,
|
||||
unit_of_measurement=pin.get(
|
||||
'unit_of_measurement'),
|
||||
corr_factor=pin.get('correction_factor',
|
||||
None),
|
||||
decimal_places=pin.get('decimal_places',
|
||||
None)))
|
||||
ATTR_UNIT_OF_MEASUREMENT),
|
||||
renderer=renderer))
|
||||
|
||||
add_devices(dev)
|
||||
|
||||
@@ -89,8 +105,7 @@ class ArestSensor(Entity):
|
||||
""" Implements an aREST sensor for exposed variables. """
|
||||
|
||||
def __init__(self, arest, resource, location, name, variable=None,
|
||||
pin=None, unit_of_measurement=None, corr_factor=None,
|
||||
decimal_places=None):
|
||||
pin=None, unit_of_measurement=None, renderer=None):
|
||||
self.arest = arest
|
||||
self._resource = resource
|
||||
self._name = '{} {}'.format(location.title(), name.title()) \
|
||||
@@ -99,8 +114,7 @@ class ArestSensor(Entity):
|
||||
self._pin = pin
|
||||
self._state = 'n/a'
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self._corr_factor = corr_factor
|
||||
self._decimal_places = decimal_places
|
||||
self._renderer = renderer
|
||||
self.update()
|
||||
|
||||
if self._pin is not None:
|
||||
@@ -126,17 +140,11 @@ class ArestSensor(Entity):
|
||||
|
||||
if 'error' in values:
|
||||
return values['error']
|
||||
elif 'value' in values:
|
||||
value = values['value']
|
||||
if self._corr_factor is not None:
|
||||
value = float(value) * float(self._corr_factor)
|
||||
if self._decimal_places is not None:
|
||||
value = round(value, self._decimal_places)
|
||||
if self._decimal_places == 0:
|
||||
value = int(value)
|
||||
return value
|
||||
else:
|
||||
return values.get(self._variable, 'n/a')
|
||||
|
||||
value = self._renderer(values.get('value',
|
||||
values.get(self._variable,
|
||||
'N/A')))
|
||||
return value
|
||||
|
||||
def update(self):
|
||||
""" Gets the latest data from aREST API. """
|
||||
|
||||
@@ -10,8 +10,9 @@ import logging
|
||||
import subprocess
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.const import CONF_VALUE_TEMPLATE
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.util import template, Throttle
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -32,25 +33,24 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
data = CommandSensorData(config.get('command'))
|
||||
|
||||
add_devices_callback([CommandSensor(
|
||||
hass,
|
||||
data,
|
||||
config.get('name', DEFAULT_NAME),
|
||||
config.get('unit_of_measurement'),
|
||||
config.get('correction_factor', None),
|
||||
config.get('decimal_places', None)
|
||||
config.get(CONF_VALUE_TEMPLATE)
|
||||
)])
|
||||
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
class CommandSensor(Entity):
|
||||
""" Represents a sensor that is returning a value of a shell commands. """
|
||||
def __init__(self, data, name, unit_of_measurement, corr_factor,
|
||||
decimal_places):
|
||||
def __init__(self, hass, data, name, unit_of_measurement, value_template):
|
||||
self._hass = hass
|
||||
self.data = data
|
||||
self._name = name
|
||||
self._state = False
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self._corr_factor = corr_factor
|
||||
self._decimal_places = decimal_places
|
||||
self._value_template = value_template
|
||||
self.update()
|
||||
|
||||
@property
|
||||
@@ -73,16 +73,10 @@ class CommandSensor(Entity):
|
||||
self.data.update()
|
||||
value = self.data.value
|
||||
|
||||
try:
|
||||
if value is not None:
|
||||
if self._corr_factor is not None:
|
||||
value = float(value) * float(self._corr_factor)
|
||||
if self._decimal_places is not None:
|
||||
value = round(value, self._decimal_places)
|
||||
if self._decimal_places == 0:
|
||||
value = int(value)
|
||||
self._state = value
|
||||
except ValueError:
|
||||
if self._value_template is not None:
|
||||
self._state = template.render_with_possible_json_value(
|
||||
self._hass, self._value_template, value, 'N/A')
|
||||
else:
|
||||
self._state = value
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
"""
|
||||
homeassistant.components.sensor.dweet
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Displays values from Dweet.io.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.dweet/
|
||||
"""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import json
|
||||
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.util import template
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.const import (STATE_UNKNOWN, CONF_VALUE_TEMPLATE)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
REQUIREMENTS = ['dweepy==0.2.0']
|
||||
|
||||
DEFAULT_NAME = 'Dweet.io Sensor'
|
||||
CONF_DEVICE = 'device'
|
||||
|
||||
# Return cached results if last scan was less then this time ago
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
||||
|
||||
|
||||
# pylint: disable=unused-variable, too-many-function-args
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Setup the Dweet sensor. """
|
||||
import dweepy
|
||||
|
||||
device = config.get('device')
|
||||
value_template = config.get(CONF_VALUE_TEMPLATE)
|
||||
|
||||
if None in (device, value_template):
|
||||
_LOGGER.error('Not all required config keys present: %s',
|
||||
', '.join(CONF_DEVICE, CONF_VALUE_TEMPLATE))
|
||||
return False
|
||||
|
||||
try:
|
||||
content = json.dumps(dweepy.get_latest_dweet_for(device)[0]['content'])
|
||||
except dweepy.DweepyError:
|
||||
_LOGGER.error("Device/thing '%s' could not be found", device)
|
||||
return False
|
||||
|
||||
if template.render_with_possible_json_value(hass,
|
||||
value_template,
|
||||
content) is '':
|
||||
_LOGGER.error("'%s' was not found", value_template)
|
||||
return False
|
||||
|
||||
dweet = DweetData(device)
|
||||
|
||||
add_devices([DweetSensor(hass,
|
||||
dweet,
|
||||
config.get('name', DEFAULT_NAME),
|
||||
value_template,
|
||||
config.get('unit_of_measurement'))])
|
||||
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
class DweetSensor(Entity):
|
||||
""" Implements a Dweet sensor. """
|
||||
|
||||
def __init__(self, hass, dweet, name, value_template, unit_of_measurement):
|
||||
self.hass = hass
|
||||
self.dweet = dweet
|
||||
self._name = name
|
||||
self._value_template = value_template
|
||||
self._state = STATE_UNKNOWN
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self.update()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" The name of the sensor. """
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
""" Unit the value is expressed in. """
|
||||
return self._unit_of_measurement
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state. """
|
||||
if self.dweet.data is None:
|
||||
return STATE_UNKNOWN
|
||||
else:
|
||||
values = json.dumps(self.dweet.data[0]['content'])
|
||||
value = template.render_with_possible_json_value(
|
||||
self.hass, self._value_template, values)
|
||||
return value
|
||||
|
||||
def update(self):
|
||||
""" Gets the latest data from REST API. """
|
||||
self.dweet.update()
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class DweetData(object):
|
||||
""" Class for handling the data retrieval. """
|
||||
|
||||
def __init__(self, device):
|
||||
self._device = device
|
||||
self.data = None
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
""" Gets the latest data from Dweet.io. """
|
||||
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
|
||||
@@ -79,7 +79,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
if resource not in SENSOR_TYPES:
|
||||
_LOGGER.error('Sensor type: "%s" does not exist', resource)
|
||||
else:
|
||||
dev.append(GlancesSensor(rest, resource))
|
||||
dev.append(GlancesSensor(rest, config.get('name'), resource))
|
||||
|
||||
add_devices(dev)
|
||||
|
||||
@@ -87,9 +87,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
class GlancesSensor(Entity):
|
||||
""" Implements a Glances sensor. """
|
||||
|
||||
def __init__(self, rest, sensor_type):
|
||||
def __init__(self, rest, name, sensor_type):
|
||||
self.rest = rest
|
||||
self._name = SENSOR_TYPES[sensor_type][0]
|
||||
self._name = name
|
||||
self.type = sensor_type
|
||||
self._state = STATE_UNKNOWN
|
||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||
@@ -98,7 +98,10 @@ class GlancesSensor(Entity):
|
||||
@property
|
||||
def name(self):
|
||||
""" The name of the sensor. """
|
||||
return self._name
|
||||
if self._name is None:
|
||||
return SENSOR_TYPES[self.type][0]
|
||||
else:
|
||||
return '{} {}'.format(self._name, SENSOR_TYPES[self.type][0])
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
"""
|
||||
homeassistant.components.sensor.rest
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
The rest sensor will consume JSON responses sent by an exposed REST API.
|
||||
The rest sensor will consume responses sent by an exposed REST API.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.rest/
|
||||
"""
|
||||
from datetime import timedelta
|
||||
from json import loads
|
||||
import logging
|
||||
|
||||
import requests
|
||||
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.const import CONF_VALUE_TEMPLATE
|
||||
from homeassistant.util import template, Throttle
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -59,57 +58,31 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
'Please check the URL in the configuration file.')
|
||||
return False
|
||||
|
||||
try:
|
||||
data = loads(response.text)
|
||||
except ValueError:
|
||||
_LOGGER.error('No valid JSON in the response in: %s', data)
|
||||
return False
|
||||
|
||||
try:
|
||||
RestSensor.extract_value(data, config.get('variable'))
|
||||
except KeyError:
|
||||
_LOGGER.error('Variable "%s" not found in response: "%s"',
|
||||
config.get('variable'), data)
|
||||
return False
|
||||
|
||||
if use_get:
|
||||
rest = RestDataGet(resource, verify_ssl)
|
||||
elif use_post:
|
||||
rest = RestDataPost(resource, payload, verify_ssl)
|
||||
|
||||
add_devices([RestSensor(rest,
|
||||
add_devices([RestSensor(hass,
|
||||
rest,
|
||||
config.get('name', DEFAULT_NAME),
|
||||
config.get('variable'),
|
||||
config.get('unit_of_measurement'),
|
||||
config.get('correction_factor', None),
|
||||
config.get('decimal_places', None))])
|
||||
config.get(CONF_VALUE_TEMPLATE))])
|
||||
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
class RestSensor(Entity):
|
||||
""" Implements a REST sensor. """
|
||||
|
||||
def __init__(self, rest, name, variable, unit_of_measurement, corr_factor,
|
||||
decimal_places):
|
||||
def __init__(self, hass, rest, name, unit_of_measurement, value_template):
|
||||
self._hass = hass
|
||||
self.rest = rest
|
||||
self._name = name
|
||||
self._variable = variable
|
||||
self._state = 'n/a'
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self._corr_factor = corr_factor
|
||||
self._decimal_places = decimal_places
|
||||
self._value_template = value_template
|
||||
self.update()
|
||||
|
||||
@classmethod
|
||||
def extract_value(cls, data, variable):
|
||||
""" Extracts the value using a key name or a path. """
|
||||
if isinstance(variable, list):
|
||||
for variable_item in variable:
|
||||
data = data[variable_item]
|
||||
return data
|
||||
else:
|
||||
return data[variable]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" The name of the sensor. """
|
||||
@@ -133,18 +106,10 @@ class RestSensor(Entity):
|
||||
if 'error' in value:
|
||||
self._state = value['error']
|
||||
else:
|
||||
try:
|
||||
if value is not None:
|
||||
value = RestSensor.extract_value(value, self._variable)
|
||||
if self._corr_factor is not None:
|
||||
value = float(value) * float(self._corr_factor)
|
||||
if self._decimal_places is not None:
|
||||
value = round(value, self._decimal_places)
|
||||
if self._decimal_places == 0:
|
||||
value = int(value)
|
||||
self._state = value
|
||||
except ValueError:
|
||||
self._state = RestSensor.extract_value(value, self._variable)
|
||||
if self._value_template is not None:
|
||||
value = template.render_with_possible_json_value(
|
||||
self._hass, self._value_template, value, 'N/A')
|
||||
self._state = value
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
@@ -164,7 +129,7 @@ class RestDataGet(object):
|
||||
verify=self._verify_ssl)
|
||||
if 'error' in self.data:
|
||||
del self.data['error']
|
||||
self.data = response.json()
|
||||
self.data = response.text
|
||||
except requests.exceptions.ConnectionError:
|
||||
_LOGGER.error("No route to resource/endpoint.")
|
||||
self.data['error'] = 'N/A'
|
||||
@@ -188,7 +153,7 @@ class RestDataPost(object):
|
||||
timeout=10, verify=self._verify_ssl)
|
||||
if 'error' in self.data:
|
||||
del self.data['error']
|
||||
self.data = response.json()
|
||||
self.data = response.text
|
||||
except requests.exceptions.ConnectionError:
|
||||
_LOGGER.error("No route to resource/endpoint.")
|
||||
self.data['error'] = 'N/A'
|
||||
@@ -0,0 +1,82 @@
|
||||
"""
|
||||
homeassistant.components.sensor.twitch
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Twitch stream status.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.twitch/
|
||||
"""
|
||||
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.const import ATTR_ENTITY_PICTURE
|
||||
|
||||
STATE_STREAMING = 'streaming'
|
||||
STATE_OFFLINE = 'offline'
|
||||
ATTR_GAME = 'game'
|
||||
ATTR_TITLE = 'title'
|
||||
ICON = 'mdi:twitch'
|
||||
|
||||
REQUIREMENTS = ['python-twitch==1.2.0']
|
||||
DOMAIN = 'twitch'
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Sets up the Twitch platform. """
|
||||
add_devices(
|
||||
[TwitchSensor(channel) for channel in config.get('channels', [])])
|
||||
|
||||
|
||||
class TwitchSensor(Entity):
|
||||
""" Represents an Twitch channel. """
|
||||
|
||||
# pylint: disable=abstract-method
|
||||
def __init__(self, channel):
|
||||
self._channel = channel
|
||||
self._state = STATE_OFFLINE
|
||||
self._preview = None
|
||||
self._game = None
|
||||
self._title = None
|
||||
self.update()
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
""" Device should be polled. """
|
||||
return True
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Returns the name of the device. """
|
||||
return self._channel
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" State of the sensor. """
|
||||
return self._state
|
||||
|
||||
# pylint: disable=no-member
|
||||
def update(self):
|
||||
""" Update device state. """
|
||||
from twitch.api import v3 as twitch
|
||||
stream = twitch.streams.by_channel(self._channel).get('stream')
|
||||
if stream:
|
||||
self._game = stream.get('channel').get('game')
|
||||
self._title = stream.get('channel').get('status')
|
||||
self._preview = stream.get('preview').get('small')
|
||||
self._state = STATE_STREAMING
|
||||
else:
|
||||
self._state = STATE_OFFLINE
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
""" Returns the state attributes. """
|
||||
if self._state == STATE_STREAMING:
|
||||
return {
|
||||
ATTR_GAME: self._game,
|
||||
ATTR_TITLE: self._title,
|
||||
ATTR_ENTITY_PICTURE: self._preview
|
||||
}
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
return ICON
|
||||
Reference in new issue
Block a user