2016-01-06 02:39:16 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.netatmo
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
NetAtmo Weather Service service.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/...
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
2016-01-13 03:16:25 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN
|
2016-01-11 05:57:31 +00:00
|
|
|
from homeassistant.const import (CONF_API_KEY, CONF_USERNAME, CONF_PASSWORD,
|
2016-01-13 03:16:25 +00:00
|
|
|
TEMP_CELCIUS)
|
2016-01-06 02:39:16 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-01-13 02:43:10 +00:00
|
|
|
from homeassistant.helpers import validate_config
|
2016-01-06 02:39:16 +00:00
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
2016-01-08 04:34:51 +00:00
|
|
|
REQUIREMENTS = [
|
|
|
|
'https://github.com/HydrelioxGitHub/netatmo-api-python/archive/'
|
2016-01-14 02:00:51 +00:00
|
|
|
'43ff238a0122b0939a0dc4e8836b6782913fb6e2.zip'
|
|
|
|
'#lnetatmo==0.4.0']
|
2016-01-08 04:34:51 +00:00
|
|
|
|
2016-01-06 02:39:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-01-13 02:45:43 +00:00
|
|
|
|
2016-01-06 02:39:16 +00:00
|
|
|
SENSOR_TYPES = {
|
2016-01-13 03:16:25 +00:00
|
|
|
'temperature': ['Temperature', TEMP_CELCIUS],
|
2016-01-13 03:26:40 +00:00
|
|
|
'co2': ['CO2', 'ppm'],
|
|
|
|
'pressure': ['Pressure', 'mb'],
|
|
|
|
'noise': ['Noise', 'dB'],
|
|
|
|
'humidity': ['Humidity', '%']
|
2016-01-06 02:39:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 02:45:43 +00:00
|
|
|
CONF_SECRET_KEY = 'secret_key'
|
2016-01-13 03:19:27 +00:00
|
|
|
ATTR_MODULE = 'modules'
|
2016-01-13 02:45:43 +00:00
|
|
|
|
2016-01-06 02:39:16 +00:00
|
|
|
# Return cached results if last scan was less then this time ago
|
2016-01-11 05:57:31 +00:00
|
|
|
# NetAtmo Data is uploaded to server every 10mn
|
|
|
|
# so this time should not be under
|
2016-01-06 02:39:16 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=600)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Get the NetAtmo sensor. """
|
|
|
|
|
2016-01-13 02:43:10 +00:00
|
|
|
if not validate_config({DOMAIN: config},
|
|
|
|
{DOMAIN: [CONF_API_KEY,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_PASSWORD,
|
2016-01-13 02:45:43 +00:00
|
|
|
CONF_SECRET_KEY]},
|
2016-01-13 02:43:10 +00:00
|
|
|
_LOGGER):
|
|
|
|
return None
|
2016-01-13 02:45:43 +00:00
|
|
|
|
2016-01-13 02:43:10 +00:00
|
|
|
import lnetatmo
|
|
|
|
|
2016-01-11 05:57:31 +00:00
|
|
|
authorization = lnetatmo.ClientAuth(config.get(CONF_API_KEY, None),
|
2016-01-13 02:45:43 +00:00
|
|
|
config.get(CONF_SECRET_KEY, None),
|
2016-01-11 05:57:31 +00:00
|
|
|
config.get(CONF_USERNAME, None),
|
|
|
|
config.get(CONF_PASSWORD, None))
|
2016-01-06 02:39:16 +00:00
|
|
|
|
|
|
|
if not authorization:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Connection error "
|
|
|
|
"Please check your settings for NatAtmo API.")
|
|
|
|
return False
|
|
|
|
|
|
|
|
data = NetAtmoData(authorization)
|
|
|
|
|
|
|
|
dev = []
|
|
|
|
try:
|
2016-01-11 05:57:31 +00:00
|
|
|
# Iterate each module
|
2016-01-13 03:19:27 +00:00
|
|
|
for module_name, monitored_conditions in config[ATTR_MODULE].items():
|
2016-01-11 05:57:31 +00:00
|
|
|
# Test if module exist """
|
|
|
|
if module_name not in data.get_module_names():
|
2016-01-11 04:21:38 +00:00
|
|
|
_LOGGER.error('Module name: "%s" not found', module_name)
|
|
|
|
continue
|
2016-01-11 05:57:31 +00:00
|
|
|
# Only create sensor for monitored """
|
2016-01-06 03:36:04 +00:00
|
|
|
for variable in monitored_conditions:
|
|
|
|
if variable not in SENSOR_TYPES:
|
|
|
|
_LOGGER.error('Sensor type: "%s" does not exist', variable)
|
|
|
|
else:
|
2016-01-11 05:57:31 +00:00
|
|
|
dev.append(
|
2016-01-13 03:16:25 +00:00
|
|
|
NetAtmoSensor(data, module_name, variable))
|
2016-01-06 02:39:16 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
add_devices(dev)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class NetAtmoSensor(Entity):
|
|
|
|
""" Implements a NetAtmo sensor. """
|
|
|
|
|
2016-01-13 03:16:25 +00:00
|
|
|
def __init__(self, netatmo_data, module_name, sensor_type):
|
2016-01-13 07:46:45 +00:00
|
|
|
self._name = "NetAtmo {} {}".format(module_name,
|
|
|
|
SENSOR_TYPES[sensor_type][0])
|
2016-01-06 02:39:16 +00:00
|
|
|
self.netatmo_data = netatmo_data
|
|
|
|
self.module_name = module_name
|
|
|
|
self.type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-01-13 07:46:45 +00:00
|
|
|
return self._name
|
2016-01-06 02:39:16 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the device. """
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
""" Unit of measurement of this entity, if any. """
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
# pylint: disable=too-many-branches
|
|
|
|
def update(self):
|
|
|
|
""" Gets the latest data from NetAtmo API and updates the states. """
|
|
|
|
|
|
|
|
self.netatmo_data.update()
|
|
|
|
data = self.netatmo_data.data[self.module_name]
|
|
|
|
|
|
|
|
if self.type == 'temperature':
|
2016-01-13 03:16:25 +00:00
|
|
|
self._state = round(data['Temperature'], 1)
|
2016-01-06 02:39:16 +00:00
|
|
|
elif self.type == 'humidity':
|
|
|
|
self._state = data['Humidity']
|
2016-01-11 04:21:38 +00:00
|
|
|
elif self.type == 'noise':
|
|
|
|
self._state = data['Noise']
|
|
|
|
elif self.type == 'co2':
|
|
|
|
self._state = data['CO2']
|
2016-01-06 02:39:16 +00:00
|
|
|
elif self.type == 'pressure':
|
2016-01-13 03:16:25 +00:00
|
|
|
self._state = round(data['Pressure'], 1)
|
2016-01-06 02:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NetAtmoData(object):
|
|
|
|
""" Gets the latest data from NetAtmo. """
|
|
|
|
|
|
|
|
def __init__(self, auth):
|
|
|
|
self.auth = auth
|
|
|
|
self.data = None
|
|
|
|
|
2016-01-11 05:57:31 +00:00
|
|
|
def get_module_names(self):
|
|
|
|
""" Return all module available on the API as a list. """
|
2016-01-11 04:21:38 +00:00
|
|
|
self.update()
|
|
|
|
return self.data.keys()
|
|
|
|
|
2016-01-06 02:39:16 +00:00
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-01-11 05:57:31 +00:00
|
|
|
""" Call the NetAtmo API to update the data. """
|
2016-01-08 05:19:03 +00:00
|
|
|
import lnetatmo
|
2016-01-11 05:57:31 +00:00
|
|
|
# Gets the latest data from NetAtmo. """
|
|
|
|
dev_list = lnetatmo.DeviceList(self.auth)
|
|
|
|
self.data = dev_list.lastData(exclude=3600)
|