core/homeassistant/components/thermostat/radiotherm.py

128 lines
3.7 KiB
Python
Raw Normal View History

2015-10-08 22:48:03 +00:00
"""
homeassistant.components.thermostat.radiotherm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Adds support for Radio Thermostat wifi-enabled home thermostats
"""
import logging
2015-10-10 16:36:34 +00:00
import datetime
from urllib.error import URLError
2015-10-08 22:48:03 +00:00
from homeassistant.components.thermostat import (ThermostatDevice, STATE_COOL,
STATE_IDLE, STATE_HEAT)
from homeassistant.const import (CONF_HOST, CONF_NAME, TEMP_FAHRENHEIT)
2015-10-10 16:36:34 +00:00
REQUIREMENTS = ['radiotherm==1.2']
2015-10-08 22:48:03 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the Radio Thermostat. """
logger = logging.getLogger(__name__)
try:
import radiotherm
except ImportError:
logger.exception(
"Error while importing dependency radiotherm. "
"Did you maybe not install the radiotherm dependency?")
return
# Detect hosts with hass discovery, config or radiotherm discovery
2015-10-10 16:36:34 +00:00
hosts = []
if discovery_info:
logger.info('hass radiotherm discovery', discovery_info)
elif CONF_HOST in config:
hosts = [config[CONF_HOST]]
else:
hosts.append(radiotherm.discover.discover_address())
2015-10-10 16:36:34 +00:00
2015-10-09 15:43:14 +00:00
name = config.get(CONF_NAME)
if hosts is None:
logger.error("no radiotherm thermostats detected")
2015-10-08 22:48:03 +00:00
return
tstats = []
for host in hosts:
try:
tstat = radiotherm.get_thermostat(host)
tstats.append(RadioThermostat(tstat))
except (URLError, OSError):
logger.exception(
"Unable to connect to Radio Thermostat @{}".format(host))
2015-10-08 22:48:03 +00:00
add_devices(tstats)
2015-10-08 22:48:03 +00:00
class RadioThermostat(ThermostatDevice):
""" Represent a Radio Thermostat. """
def __init__(self, device, name=None):
self.device = device
2015-10-09 15:43:14 +00:00
if name:
self.set_name(name)
2015-10-10 16:36:34 +00:00
self.set_time()
2015-10-08 22:48:03 +00:00
@property
def name(self):
""" Returns the name of the Radio Thermostat. """
2015-10-09 15:43:14 +00:00
return self.device.name['raw']
2015-10-08 22:48:03 +00:00
@property
def unit_of_measurement(self):
""" Unit of measurement this thermostat expresses itself in. """
return TEMP_FAHRENHEIT
@property
def device_state_attributes(self):
""" Returns device specific state attributes. """
# Move these to Thermostat Device and make them global
return {
"humidity": None,
"target_humidity": None,
"fan": self.device.fmode['human'],
"mode": self.device.tmode['human']
}
@property
def current_temperature(self):
""" Returns the current temperature. """
return self.device.temp['raw']
@property
def operation(self):
""" Returns current operation. head, cool idle """
if self.device.tmode['human'] == 'Cool':
return STATE_COOL
elif self.device.tmode['human'] == 'Heat':
return STATE_HEAT
else:
return STATE_IDLE
@property
def target_temperature(self):
""" Returns the temperature we try to reach. """
if self.operation == STATE_COOL:
temp = self.device.t_cool['raw']
elif self.operation == STATE_HEAT:
temp = self.device.t_heat['raw']
return round(temp, 1)
2015-10-09 15:43:14 +00:00
def set_temperature(self, temperature):
2015-10-08 22:48:03 +00:00
""" Set new target temperature """
if self.operation == STATE_COOL:
self.device.t_cool = temperature
elif self.operation == STATE_HEAT:
2015-10-09 16:34:14 +00:00
self.device.t_heat = temperature
2015-10-08 22:48:03 +00:00
2015-10-09 15:43:14 +00:00
def set_name(self, name):
""" Set thermostat name """
self.device.name = name
2015-10-10 16:36:34 +00:00
def set_time(self):
""" Set device time """
now = datetime.datetime.now()
self.device.time = {'day': now.weekday(),
'hour': now.hour, 'minute': now.minute}