Tado climate device (#6572)
* Added tado climate component named the component v1 because of the unsupported state of the api I used (mytado.com) * sensor component * climate component which uses sensors * main component initiating sensor and climate devices * order of imports * consts for username and password * removed redundant code * changed wrong calls and properties * remove pylint overrides * merged update() and push_update() * changed wrong calls * removed pylint overrides * moved try..except * renamed MyTado hass-data object * added TadoDataStore * moved update methods from sensor to TadoDataStore * reorganised climate component * use new TadoDataStore * small change to overlay handling * code refactoring * removed unnessesary comments * changed throttle to attribute * changed suggestions from PR * Added constant variable for string literal * remove wrong fget() call * changed dependencies * Changed operation mode list * added human readable list of operations * removed unnecessary const * activated update on add_devices * droped unit * removed unnused property * changed temperature conversion * removed defaults from config changed naming of tado data const * switched operation_list key/values * changed the value returned as state * added one extra line * dropped state to use base impl. * renamed component * had to inplement temperature_unit * because it is not implemented in base class * create a copy of the sensors list * because it can be changed by other components * had to check for empty data object * hass is too fast now
This commit is contained in:
parent
e7425e9808
commit
f4aec3ac88
5 changed files with 642 additions and 0 deletions
209
homeassistant/components/sensor/tado.py
Normal file
209
homeassistant/components/sensor/tado.py
Normal file
|
@ -0,0 +1,209 @@
|
|||
"""tado component to create some sensors for each zone."""
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.components.tado import (
|
||||
DATA_TADO)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
SENSOR_TYPES = ['temperature', 'humidity', 'power',
|
||||
'link', 'heating', 'tado mode', 'overlay']
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the sensor platform."""
|
||||
# get the PyTado object from the hub component
|
||||
tado = hass.data[DATA_TADO]
|
||||
|
||||
try:
|
||||
zones = tado.get_zones()
|
||||
except RuntimeError:
|
||||
_LOGGER.error("Unable to get zone info from mytado")
|
||||
return False
|
||||
|
||||
sensor_items = []
|
||||
for zone in zones:
|
||||
if zone['type'] == 'HEATING':
|
||||
for variable in SENSOR_TYPES:
|
||||
sensor_items.append(create_zone_sensor(
|
||||
tado, zone, zone['name'], zone['id'],
|
||||
variable))
|
||||
|
||||
me_data = tado.get_me()
|
||||
sensor_items.append(create_device_sensor(
|
||||
tado, me_data, me_data['homes'][0]['name'],
|
||||
me_data['homes'][0]['id'], "tado bridge status"))
|
||||
|
||||
if len(sensor_items) > 0:
|
||||
add_devices(sensor_items, True)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def create_zone_sensor(tado, zone, name, zone_id, variable):
|
||||
"""Create a zone sensor."""
|
||||
data_id = 'zone {} {}'.format(name, zone_id)
|
||||
|
||||
tado.add_sensor(data_id, {
|
||||
"zone": zone,
|
||||
"name": name,
|
||||
"id": zone_id,
|
||||
"data_id": data_id
|
||||
})
|
||||
|
||||
return TadoSensor(tado, name, zone_id, variable, data_id)
|
||||
|
||||
|
||||
def create_device_sensor(tado, device, name, device_id, variable):
|
||||
"""Create a device sensor."""
|
||||
data_id = 'device {} {}'.format(name, device_id)
|
||||
|
||||
tado.add_sensor(data_id, {
|
||||
"device": device,
|
||||
"name": name,
|
||||
"id": device_id,
|
||||
"data_id": data_id
|
||||
})
|
||||
|
||||
return TadoSensor(tado, name, device_id, variable, data_id)
|
||||
|
||||
|
||||
class TadoSensor(Entity):
|
||||
"""Representation of a tado Sensor."""
|
||||
|
||||
def __init__(self, store, zone_name, zone_id, zone_variable, data_id):
|
||||
"""Initialization of TadoSensor class."""
|
||||
self._store = store
|
||||
|
||||
self.zone_name = zone_name
|
||||
self.zone_id = zone_id
|
||||
self.zone_variable = zone_variable
|
||||
|
||||
self._unique_id = '{} {}'.format(zone_variable, zone_id)
|
||||
self._data_id = data_id
|
||||
|
||||
self._state = None
|
||||
self._state_attributes = None
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique id."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return '{} {}'.format(self.zone_name, self.zone_variable)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return self._state_attributes
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement."""
|
||||
if self.zone_variable == "temperature":
|
||||
return self.hass.config.units.temperature_unit
|
||||
elif self.zone_variable == "humidity":
|
||||
return '%'
|
||||
elif self.zone_variable == "heating":
|
||||
return '%'
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon for the sensor."""
|
||||
if self.zone_variable == "temperature":
|
||||
return 'mdi:thermometer'
|
||||
elif self.zone_variable == "humidity":
|
||||
return 'mdi:water-percent'
|
||||
|
||||
def update(self):
|
||||
"""Update method called when should_poll is true."""
|
||||
self._store.update()
|
||||
|
||||
data = self._store.get_data(self._data_id)
|
||||
|
||||
if data is None:
|
||||
_LOGGER.debug('Recieved no data for zone %s',
|
||||
self.zone_name)
|
||||
return
|
||||
|
||||
unit = TEMP_CELSIUS
|
||||
|
||||
# pylint: disable=R0912
|
||||
if self.zone_variable == 'temperature':
|
||||
if 'sensorDataPoints' in data:
|
||||
sensor_data = data['sensorDataPoints']
|
||||
temperature = float(
|
||||
sensor_data['insideTemperature']['celsius'])
|
||||
|
||||
self._state = self.hass.config.units.temperature(
|
||||
temperature, unit)
|
||||
self._state_attributes = {
|
||||
"time":
|
||||
sensor_data['insideTemperature']['timestamp'],
|
||||
"setting": 0 # setting is used in climate device
|
||||
}
|
||||
|
||||
# temperature setting will not exist when device is off
|
||||
if 'temperature' in data['setting'] and \
|
||||
data['setting']['temperature'] is not None:
|
||||
temperature = float(
|
||||
data['setting']['temperature']['celsius'])
|
||||
|
||||
self._state_attributes["setting"] = \
|
||||
self.hass.config.units.temperature(
|
||||
temperature, unit)
|
||||
|
||||
elif self.zone_variable == 'humidity':
|
||||
if 'sensorDataPoints' in data:
|
||||
sensor_data = data['sensorDataPoints']
|
||||
self._state = float(
|
||||
sensor_data['humidity']['percentage'])
|
||||
self._state_attributes = {
|
||||
"time": sensor_data['humidity']['timestamp'],
|
||||
}
|
||||
|
||||
elif self.zone_variable == 'power':
|
||||
if 'setting' in data:
|
||||
self._state = data['setting']['power']
|
||||
|
||||
elif self.zone_variable == 'link':
|
||||
if 'link' in data:
|
||||
self._state = data['link']['state']
|
||||
|
||||
elif self.zone_variable == 'heating':
|
||||
if 'activityDataPoints' in data:
|
||||
activity_data = data['activityDataPoints']
|
||||
self._state = float(
|
||||
activity_data['heatingPower']['percentage'])
|
||||
self._state_attributes = {
|
||||
"time": activity_data['heatingPower']['timestamp'],
|
||||
}
|
||||
|
||||
elif self.zone_variable == 'tado bridge status':
|
||||
if 'connectionState' in data:
|
||||
self._state = data['connectionState']['value']
|
||||
|
||||
elif self.zone_variable == 'tado mode':
|
||||
if 'tadoMode' in data:
|
||||
self._state = data['tadoMode']
|
||||
|
||||
elif self.zone_variable == 'overlay':
|
||||
if 'overlay' in data and data['overlay'] is not None:
|
||||
self._state = True
|
||||
self._state_attributes = {
|
||||
"termination": data['overlay']['termination']['type'],
|
||||
}
|
||||
else:
|
||||
self._state = False
|
||||
self._state_attributes = {}
|
Loading…
Add table
Add a link
Reference in a new issue