Merge branch 'dev' into mysensors-component-switch
Conflicts: homeassistant/components/sensor/__init__.py homeassistant/components/switch/__init__.py
This commit is contained in:
commit
6ff24ed047
47 files changed
+1171
-427
No files matched your search
@@ -10,7 +10,7 @@ import logging
|
||||
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.components import (
|
||||
wink, zwave, isy994, verisure, ecobee, mysensors)
|
||||
wink, zwave, isy994, verisure, ecobee, tellduslive, mysensors)
|
||||
|
||||
DOMAIN = 'sensor'
|
||||
SCAN_INTERVAL = 30
|
||||
@@ -24,6 +24,7 @@ DISCOVERY_PLATFORMS = {
|
||||
isy994.DISCOVER_SENSORS: 'isy994',
|
||||
verisure.DISCOVER_SENSORS: 'verisure',
|
||||
ecobee.DISCOVER_SENSORS: 'ecobee',
|
||||
tellduslive.DISCOVER_SENSORS: 'tellduslive',
|
||||
mysensors.DISCOVER_SENSORS: 'mysensors',
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,11 @@ class EliqSensor(Entity):
|
||||
""" Returns the name. """
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
""" Returns icon. """
|
||||
return "mdi:speedometer"
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
""" Unit of measurement of this entity, if any. """
|
||||
|
||||
@@ -102,13 +102,13 @@ class RestSensor(Entity):
|
||||
self.rest.update()
|
||||
value = self.rest.data
|
||||
|
||||
if 'error' in value:
|
||||
self._state = value['error']
|
||||
else:
|
||||
if self._value_template is not None:
|
||||
value = template.render_with_possible_json_value(
|
||||
self._hass, self._value_template, value, STATE_UNKNOWN)
|
||||
self._state = value
|
||||
if value is None:
|
||||
value = STATE_UNKNOWN
|
||||
elif self._value_template is not None:
|
||||
value = template.render_with_possible_json_value(
|
||||
self._hass, self._value_template, value, STATE_UNKNOWN)
|
||||
|
||||
self._state = value
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
@@ -118,7 +118,7 @@ class RestDataGet(object):
|
||||
def __init__(self, resource, verify_ssl):
|
||||
self._resource = resource
|
||||
self._verify_ssl = verify_ssl
|
||||
self.data = dict()
|
||||
self.data = None
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
@@ -126,12 +126,10 @@ class RestDataGet(object):
|
||||
try:
|
||||
response = requests.get(self._resource, timeout=10,
|
||||
verify=self._verify_ssl)
|
||||
if 'error' in self.data:
|
||||
del self.data['error']
|
||||
self.data = response.text
|
||||
except requests.exceptions.ConnectionError:
|
||||
_LOGGER.error("No route to resource/endpoint: %s", self._resource)
|
||||
self.data['error'] = STATE_UNKNOWN
|
||||
self.data = None
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
@@ -142,7 +140,7 @@ class RestDataPost(object):
|
||||
self._resource = resource
|
||||
self._payload = payload
|
||||
self._verify_ssl = verify_ssl
|
||||
self.data = dict()
|
||||
self.data = None
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
@@ -150,9 +148,7 @@ class RestDataPost(object):
|
||||
try:
|
||||
response = requests.post(self._resource, data=self._payload,
|
||||
timeout=10, verify=self._verify_ssl)
|
||||
if 'error' in self.data:
|
||||
del self.data['error']
|
||||
self.data = response.text
|
||||
except requests.exceptions.ConnectionError:
|
||||
_LOGGER.error("No route to resource/endpoint: %s", self._resource)
|
||||
self.data['error'] = STATE_UNKNOWN
|
||||
self.data = None
|
||||
@@ -14,25 +14,25 @@ from homeassistant.const import STATE_ON, STATE_OFF
|
||||
|
||||
REQUIREMENTS = ['psutil==3.2.2']
|
||||
SENSOR_TYPES = {
|
||||
'disk_use_percent': ['Disk Use', '%'],
|
||||
'disk_use': ['Disk Use', 'GiB'],
|
||||
'disk_free': ['Disk Free', 'GiB'],
|
||||
'memory_use_percent': ['RAM Use', '%'],
|
||||
'memory_use': ['RAM Use', 'MiB'],
|
||||
'memory_free': ['RAM Free', 'MiB'],
|
||||
'processor_use': ['CPU Use', '%'],
|
||||
'process': ['Process', ''],
|
||||
'swap_use_percent': ['Swap Use', '%'],
|
||||
'swap_use': ['Swap Use', 'GiB'],
|
||||
'swap_free': ['Swap Free', 'GiB'],
|
||||
'network_out': ['Sent', 'MiB'],
|
||||
'network_in': ['Recieved', 'MiB'],
|
||||
'packets_out': ['Packets sent', ''],
|
||||
'packets_in': ['Packets recieved', ''],
|
||||
'ipv4_address': ['IPv4 address', ''],
|
||||
'ipv6_address': ['IPv6 address', ''],
|
||||
'last_boot': ['Last Boot', ''],
|
||||
'since_last_boot': ['Since Last Boot', '']
|
||||
'disk_use_percent': ['Disk Use', '%', 'mdi:harddisk'],
|
||||
'disk_use': ['Disk Use', 'GiB', 'mdi:harddisk'],
|
||||
'disk_free': ['Disk Free', 'GiB', 'mdi:harddisk'],
|
||||
'memory_use_percent': ['RAM Use', '%', 'mdi:memory'],
|
||||
'memory_use': ['RAM Use', 'MiB', 'mdi:memory'],
|
||||
'memory_free': ['RAM Free', 'MiB', 'mdi:memory'],
|
||||
'processor_use': ['CPU Use', '%', 'mdi:memory'],
|
||||
'process': ['Process', '', 'mdi:memory'],
|
||||
'swap_use_percent': ['Swap Use', '%', 'mdi:harddisk'],
|
||||
'swap_use': ['Swap Use', 'GiB', 'mdi:harddisk'],
|
||||
'swap_free': ['Swap Free', 'GiB', 'mdi:harddisk'],
|
||||
'network_out': ['Sent', 'MiB', 'mdi:server-network'],
|
||||
'network_in': ['Recieved', 'MiB', 'mdi:server-network'],
|
||||
'packets_out': ['Packets sent', '', 'mdi:server-network'],
|
||||
'packets_in': ['Packets recieved', '', 'mdi:server-network'],
|
||||
'ipv4_address': ['IPv4 address', '', 'mdi:server-network'],
|
||||
'ipv6_address': ['IPv6 address', '', 'mdi:server-network'],
|
||||
'last_boot': ['Last Boot', '', 'mdi:clock'],
|
||||
'since_last_boot': ['Since Last Boot', '', 'mdi:clock']
|
||||
}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -69,6 +69,10 @@ class SystemMonitorSensor(Entity):
|
||||
def name(self):
|
||||
return self._name.rstrip()
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
return SENSOR_TYPES[self.type][2]
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state of the device. """
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
homeassistant.components.sensor.tellduslive
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Shows sensor values from Tellstick Net/Telstick Live.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.tellduslive/
|
||||
|
||||
"""
|
||||
import logging
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from homeassistant.const import TEMP_CELCIUS, ATTR_BATTERY_LEVEL
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.components import tellduslive
|
||||
|
||||
ATTR_LAST_UPDATED = "time_last_updated"
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DEPENDENCIES = ['tellduslive']
|
||||
|
||||
SENSOR_TYPE_TEMP = "temp"
|
||||
SENSOR_TYPE_HUMIDITY = "humidity"
|
||||
|
||||
SENSOR_TYPES = {
|
||||
SENSOR_TYPE_TEMP: ['Temperature', TEMP_CELCIUS, "mdi:thermometer"],
|
||||
SENSOR_TYPE_HUMIDITY: ['Humidity', '%', "mdi:water"],
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Sets up Tellstick sensors. """
|
||||
sensors = tellduslive.NETWORK.get_sensors()
|
||||
devices = []
|
||||
|
||||
for component in sensors:
|
||||
for sensor in component["data"]:
|
||||
# one component can have more than one sensor
|
||||
# (e.g. both humidity and temperature)
|
||||
devices.append(TelldusLiveSensor(component["id"],
|
||||
component["name"],
|
||||
sensor["name"]))
|
||||
add_devices(devices)
|
||||
|
||||
|
||||
class TelldusLiveSensor(Entity):
|
||||
""" Represents a Telldus Live sensor. """
|
||||
|
||||
def __init__(self, sensor_id, sensor_name, sensor_type):
|
||||
self._sensor_id = sensor_id
|
||||
self._sensor_type = sensor_type
|
||||
self._state = None
|
||||
self._name = sensor_name + ' ' + SENSOR_TYPES[sensor_type][0]
|
||||
self._last_update = None
|
||||
self._battery_level = None
|
||||
self.update()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Returns the name of the device. """
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state of the device. """
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
attrs = dict()
|
||||
if self._battery_level is not None:
|
||||
attrs[ATTR_BATTERY_LEVEL] = self._battery_level
|
||||
if self._last_update is not None:
|
||||
attrs[ATTR_LAST_UPDATED] = self._last_update
|
||||
return attrs
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
return SENSOR_TYPES[self._sensor_type][1]
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
return SENSOR_TYPES[self._sensor_type][2]
|
||||
|
||||
def update(self):
|
||||
values = tellduslive.NETWORK.get_sensor_value(self._sensor_id,
|
||||
self._sensor_type)
|
||||
self._state, self._battery_level, self._last_update = values
|
||||
|
||||
self._state = float(self._state)
|
||||
if self._sensor_type == SENSOR_TYPE_TEMP:
|
||||
self._state = round(self._state, 1)
|
||||
elif self._sensor_type == SENSOR_TYPE_HUMIDITY:
|
||||
self._state = int(round(self._state))
|
||||
|
||||
self._battery_level = round(self._battery_level * 100 / 255) # percent
|
||||
|
||||
self._last_update = str(datetime.fromtimestamp(self._last_update))
|
||||
@@ -27,14 +27,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
sensors.extend([
|
||||
VerisureThermometer(value)
|
||||
for value in verisure.get_climate_status().values()
|
||||
for value in verisure.CLIMATE_STATUS.values()
|
||||
if verisure.SHOW_THERMOMETERS and
|
||||
hasattr(value, 'temperature') and value.temperature
|
||||
])
|
||||
|
||||
sensors.extend([
|
||||
VerisureHygrometer(value)
|
||||
for value in verisure.get_climate_status().values()
|
||||
for value in verisure.CLIMATE_STATUS.values()
|
||||
if verisure.SHOW_HYGROMETERS and
|
||||
hasattr(value, 'humidity') and value.humidity
|
||||
])
|
||||
@@ -47,20 +47,19 @@ class VerisureThermometer(Entity):
|
||||
|
||||
def __init__(self, climate_status):
|
||||
self._id = climate_status.id
|
||||
self._device = verisure.MY_PAGES.DEVICE_CLIMATE
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Returns the name of the device. """
|
||||
return '{} {}'.format(
|
||||
verisure.STATUS[self._device][self._id].location,
|
||||
verisure.CLIMATE_STATUS[self._id].location,
|
||||
"Temperature")
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state of the device. """
|
||||
# remove ° character
|
||||
return verisure.STATUS[self._device][self._id].temperature[:-1]
|
||||
return verisure.CLIMATE_STATUS[self._id].temperature[:-1]
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
@@ -69,7 +68,7 @@ class VerisureThermometer(Entity):
|
||||
|
||||
def update(self):
|
||||
''' update sensor '''
|
||||
verisure.update()
|
||||
verisure.update_climate()
|
||||
|
||||
|
||||
class VerisureHygrometer(Entity):
|
||||
@@ -77,20 +76,19 @@ class VerisureHygrometer(Entity):
|
||||
|
||||
def __init__(self, climate_status):
|
||||
self._id = climate_status.id
|
||||
self._device = verisure.MY_PAGES.DEVICE_CLIMATE
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Returns the name of the device. """
|
||||
return '{} {}'.format(
|
||||
verisure.STATUS[self._device][self._id].location,
|
||||
verisure.CLIMATE_STATUS[self._id].location,
|
||||
"Humidity")
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state of the device. """
|
||||
# remove % character
|
||||
return verisure.STATUS[self._device][self._id].humidity[:-1]
|
||||
return verisure.CLIMATE_STATUS[self._id].humidity[:-1]
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
@@ -99,4 +97,4 @@ class VerisureHygrometer(Entity):
|
||||
|
||||
def update(self):
|
||||
''' update sensor '''
|
||||
verisure.update()
|
||||
verisure.update_climate()
|
||||
@@ -36,17 +36,17 @@ sensor:
|
||||
|
||||
"""
|
||||
import logging
|
||||
import datetime
|
||||
import urllib.request
|
||||
|
||||
import requests
|
||||
|
||||
from homeassistant.const import ATTR_ENTITY_PICTURE
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import location, dt as dt_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
REQUIREMENTS = ['xmltodict', 'astral==0.8.1']
|
||||
REQUIREMENTS = ['xmltodict']
|
||||
|
||||
# Sensor types are defined like so:
|
||||
SENSOR_TYPES = {
|
||||
@@ -73,19 +73,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
||||
return False
|
||||
|
||||
from astral import Location, GoogleGeocoder
|
||||
location = Location(('', '', hass.config.latitude, hass.config.longitude,
|
||||
hass.config.time_zone, 0))
|
||||
elevation = config.get('elevation')
|
||||
|
||||
google = GoogleGeocoder()
|
||||
try:
|
||||
google._get_elevation(location) # pylint: disable=protected-access
|
||||
_LOGGER.info(
|
||||
'Retrieved elevation from Google: %s', location.elevation)
|
||||
elevation = location.elevation
|
||||
except urllib.error.URLError:
|
||||
# If no internet connection available etc.
|
||||
elevation = 0
|
||||
if elevation is None:
|
||||
elevation = location.elevation(hass.config.latitude,
|
||||
hass.config.longitude)
|
||||
|
||||
coordinates = dict(lat=hass.config.latitude,
|
||||
lon=hass.config.longitude, msl=elevation)
|
||||
@@ -116,9 +108,8 @@ class YrSensor(Entity):
|
||||
self.type = sensor_type
|
||||
self._state = None
|
||||
self._weather = weather
|
||||
self._info = ''
|
||||
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
|
||||
self._update = datetime.datetime.fromtimestamp(0)
|
||||
self._update = None
|
||||
|
||||
self.update()
|
||||
|
||||
@@ -134,14 +125,15 @@ class YrSensor(Entity):
|
||||
@property
|
||||
def state_attributes(self):
|
||||
""" Returns state attributes. """
|
||||
data = {}
|
||||
data[''] = "Weather forecast from yr.no, delivered by the"\
|
||||
" Norwegian Meteorological Institute and the NRK"
|
||||
data = {
|
||||
'about': "Weather forecast from yr.no, delivered by the"
|
||||
" Norwegian Meteorological Institute and the NRK"
|
||||
}
|
||||
if self.type == 'symbol':
|
||||
symbol_nr = self._state
|
||||
data[ATTR_ENTITY_PICTURE] = "http://api.met.no/weatherapi/weathericon/1.1/" \
|
||||
"?symbol=" + str(symbol_nr) + \
|
||||
";content_type=image/png"
|
||||
data[ATTR_ENTITY_PICTURE] = \
|
||||
"http://api.met.no/weatherapi/weathericon/1.1/" \
|
||||
"?symbol={0};content_type=image/png".format(symbol_nr)
|
||||
|
||||
return data
|
||||
|
||||
@@ -150,76 +142,50 @@ class YrSensor(Entity):
|
||||
""" Unit of measurement of this entity, if any. """
|
||||
return self._unit_of_measurement
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
""" Return True if entity has to be polled for state. """
|
||||
return True
|
||||
|
||||
# pylint: disable=too-many-branches, too-many-return-statements
|
||||
def update(self):
|
||||
""" Gets the latest data from yr.no and updates the states. """
|
||||
|
||||
self._weather.update()
|
||||
now = datetime.datetime.now()
|
||||
now = dt_util.utcnow()
|
||||
# check if data should be updated
|
||||
if now <= self._update:
|
||||
if self._update is not None and now <= self._update:
|
||||
return
|
||||
|
||||
time_data = self._weather.data['product']['time']
|
||||
self._weather.update()
|
||||
|
||||
# pylint: disable=consider-using-enumerate
|
||||
# find sensor
|
||||
for k in range(len(time_data)):
|
||||
valid_from = datetime.datetime.strptime(time_data[k]['@from'],
|
||||
"%Y-%m-%dT%H:%M:%SZ")
|
||||
valid_to = datetime.datetime.strptime(time_data[k]['@to'],
|
||||
"%Y-%m-%dT%H:%M:%SZ")
|
||||
self._update = valid_to
|
||||
self._info = "Forecast between " + time_data[k]['@from'] \
|
||||
+ " and " + time_data[k]['@to'] + ". "
|
||||
for time_entry in self._weather.data['product']['time']:
|
||||
valid_from = dt_util.str_to_datetime(
|
||||
time_entry['@from'], "%Y-%m-%dT%H:%M:%SZ")
|
||||
valid_to = dt_util.str_to_datetime(
|
||||
time_entry['@to'], "%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
temp_data = time_data[k]['location']
|
||||
if self.type not in temp_data and now >= valid_to:
|
||||
loc_data = time_entry['location']
|
||||
|
||||
if self.type not in loc_data or now >= valid_to:
|
||||
continue
|
||||
|
||||
self._update = valid_to
|
||||
|
||||
if self.type == 'precipitation' and valid_from < now:
|
||||
self._state = temp_data[self.type]['@value']
|
||||
return
|
||||
self._state = loc_data[self.type]['@value']
|
||||
break
|
||||
elif self.type == 'symbol' and valid_from < now:
|
||||
self._state = temp_data[self.type]['@number']
|
||||
return
|
||||
elif self.type == 'temperature':
|
||||
self._state = temp_data[self.type]['@value']
|
||||
return
|
||||
self._state = loc_data[self.type]['@number']
|
||||
break
|
||||
elif self.type == ('temperature', 'pressure', 'humidity',
|
||||
'dewpointTemperature'):
|
||||
self._state = loc_data[self.type]['@value']
|
||||
break
|
||||
elif self.type == 'windSpeed':
|
||||
self._state = temp_data[self.type]['@mps']
|
||||
return
|
||||
elif self.type == 'pressure':
|
||||
self._state = temp_data[self.type]['@value']
|
||||
return
|
||||
self._state = loc_data[self.type]['@mps']
|
||||
break
|
||||
elif self.type == 'windDirection':
|
||||
self._state = float(temp_data[self.type]['@deg'])
|
||||
return
|
||||
elif self.type == 'humidity':
|
||||
self._state = temp_data[self.type]['@value']
|
||||
return
|
||||
elif self.type == 'fog':
|
||||
self._state = temp_data[self.type]['@percent']
|
||||
return
|
||||
elif self.type == 'cloudiness':
|
||||
self._state = temp_data[self.type]['@percent']
|
||||
return
|
||||
elif self.type == 'lowClouds':
|
||||
self._state = temp_data[self.type]['@percent']
|
||||
return
|
||||
elif self.type == 'mediumClouds':
|
||||
self._state = temp_data[self.type]['@percent']
|
||||
return
|
||||
elif self.type == 'highClouds':
|
||||
self._state = temp_data[self.type]['@percent']
|
||||
return
|
||||
elif self.type == 'dewpointTemperature':
|
||||
self._state = temp_data[self.type]['@value']
|
||||
return
|
||||
self._state = float(loc_data[self.type]['@deg'])
|
||||
break
|
||||
elif self.type in ('fog', 'cloudiness', 'lowClouds',
|
||||
'mediumClouds', 'highClouds'):
|
||||
self._state = loc_data[self.type]['@percent']
|
||||
break
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
@@ -230,17 +196,18 @@ class YrData(object):
|
||||
self._url = 'http://api.yr.no/weatherapi/locationforecast/1.9/?' \
|
||||
'lat={lat};lon={lon};msl={msl}'.format(**coordinates)
|
||||
|
||||
self._nextrun = datetime.datetime.fromtimestamp(0)
|
||||
self._nextrun = None
|
||||
self.data = {}
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
""" Gets the latest data from yr.no """
|
||||
now = datetime.datetime.now()
|
||||
# check if new will be available
|
||||
if now <= self._nextrun:
|
||||
if self._nextrun is not None and dt_util.utcnow() <= self._nextrun:
|
||||
return
|
||||
try:
|
||||
response = requests.get(self._url)
|
||||
with requests.Session() as sess:
|
||||
response = sess.get(self._url)
|
||||
except requests.RequestException:
|
||||
return
|
||||
if response.status_code != 200:
|
||||
@@ -252,5 +219,5 @@ class YrData(object):
|
||||
model = self.data['meta']['model']
|
||||
if '@nextrun' not in model:
|
||||
model = model[0]
|
||||
self._nextrun = datetime.datetime.strptime(model['@nextrun'],
|
||||
"%Y-%m-%dT%H:%M:%SZ")
|
||||
self._nextrun = dt_util.str_to_datetime(model['@nextrun'],
|
||||
"%Y-%m-%dT%H:%M:%SZ")
|
||||
Reference in new issue
Block a user