"""
Support for Radio Thermostat wifi-enabled home thermostats.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/climate.radiotherm/
"""
import datetime
import logging
from urllib.error import URLError

from homeassistant.components.climate import (
    STATE_AUTO, STATE_COOL, STATE_HEAT, STATE_IDLE, STATE_OFF,
    ClimateDevice)
from homeassistant.const import CONF_HOST, TEMP_FAHRENHEIT, ATTR_TEMPERATURE

REQUIREMENTS = ['radiotherm==1.2']
HOLD_TEMP = 'hold_temp'
_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the Radio Thermostat."""
    import radiotherm

    hosts = []
    if CONF_HOST in config:
        hosts = config[CONF_HOST]
    else:
        hosts.append(radiotherm.discover.discover_address())

    if hosts is None:
        _LOGGER.error("No radiotherm thermostats detected.")
        return False

    hold_temp = config.get(HOLD_TEMP, False)
    tstats = []

    for host in hosts:
        try:
            tstat = radiotherm.get_thermostat(host)
            tstats.append(RadioThermostat(tstat, hold_temp))
        except (URLError, OSError):
            _LOGGER.exception("Unable to connect to Radio Thermostat: %s",
                              host)

    add_devices(tstats)


# pylint: disable=abstract-method
class RadioThermostat(ClimateDevice):
    """Representation of a Radio Thermostat."""

    def __init__(self, device, hold_temp):
        """Initialize the thermostat."""
        self.device = device
        self.set_time()
        self._target_temperature = None
        self._current_temperature = None
        self._current_operation = STATE_IDLE
        self._name = None
        self.hold_temp = hold_temp
        self.update()

    @property
    def name(self):
        """Return the name of the Radio Thermostat."""
        return self._name

    @property
    def unit_of_measurement(self):
        """Return the unit of measurement."""
        return TEMP_FAHRENHEIT

    @property
    def device_state_attributes(self):
        """Return the device specific state attributes."""
        return {
            "fan": self.device.fmode['human'],
            "mode": self.device.tmode['human']
        }

    @property
    def current_temperature(self):
        """Return the current temperature."""
        return self._current_temperature

    @property
    def current_operation(self):
        """Return the current operation. head, cool idle."""
        return self._current_operation

    @property
    def target_temperature(self):
        """Return the temperature we try to reach."""
        return self._target_temperature

    def update(self):
        """Update the data from the thermostat."""
        self._current_temperature = self.device.temp['raw']
        self._name = self.device.name['raw']
        if self.device.tmode['human'] == 'Cool':
            self._target_temperature = self.device.t_cool['raw']
            self._current_operation = STATE_COOL
        elif self.device.tmode['human'] == 'Heat':
            self._target_temperature = self.device.t_heat['raw']
            self._current_operation = STATE_HEAT
        else:
            self._current_operation = STATE_IDLE

    def set_temperature(self, **kwargs):
        """Set new target temperature."""
        temperature = kwargs.get(ATTR_TEMPERATURE)
        if temperature is None:
            return
        if self._current_operation == STATE_COOL:
            self.device.t_cool = temperature
        elif self._current_operation == STATE_HEAT:
            self.device.t_heat = temperature
        if self.hold_temp:
            self.device.hold = 1
        else:
            self.device.hold = 0

    def set_time(self):
        """Set device time."""
        now = datetime.datetime.now()
        self.device.time = {'day': now.weekday(),
                            'hour': now.hour, 'minute': now.minute}

    def set_operation_mode(self, operation_mode):
        """Set operation mode (auto, cool, heat, off)."""
        if operation_mode == STATE_OFF:
            self.device.tmode = 0
        elif operation_mode == STATE_AUTO:
            self.device.tmode = 3
        elif operation_mode == STATE_COOL:
            self.device.t_cool = self._target_temperature
        elif operation_mode == STATE_HEAT:
            self.device.t_heat = self._target_temperature

@@ -260,16 +260,42 @@
 LError%0A%0A
+import voluptuous as vol%0A%0A
 from hom
@@ -413,16 +413,33 @@
 teDevice
+, PLATFORM_SCHEMA
 )%0Afrom h
@@ -508,16 +508,69 @@
 PERATURE
+%0Aimport homeassistant.helpers.config_validation as cv
 %0A%0AREQUIR
@@ -602,31 +602,8 @@
 2'%5D%0A
-HOLD_TEMP = 'hold_temp'
 %0A_LO
@@ -638,16 +638,258 @@
 ame__)%0A%0A
+ATTR_FAN = 'fan'%0AATTR_MODE = 'mode'%0A%0ACONF_HOLD_TEMP = 'hold_temp'%0A%0APLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(%7B%0A    vol.Optional(CONF_HOST): vol.All(cv.ensure_list, %5Bcv.string%5D),%0A    vol.Optional(CONF_HOLD_TEMP, default=False): cv.boolean,%0A%7D)%0A%0A
 %0Adef set
@@ -1207,17 +1207,17 @@
 ror(%22No 
-r
+R
 adiother
@@ -1218,17 +1218,17 @@
 iotherm 
-t
+T
 hermosta
@@ -1238,17 +1238,16 @@
 detected
-.
 %22)%0A     
@@ -1290,16 +1290,21 @@
 fig.get(
+CONF_
 HOLD_TEM
@@ -1308,15 +1308,8 @@
 TEMP
-, False
 )%0A  
@@ -2502,13 +2502,16 @@
     
-%22fan%22
+ATTR_FAN
 : se
@@ -2552,14 +2552,17 @@
     
-%22mode%22
+ATTR_MODE
 : se
@@ -4201,16 +4201,29 @@
 time = %7B
+%0A            
 'day': n
@@ -4236,32 +4236,16 @@
 kday(),%0A
-                
         
@@ -4265,16 +4265,28 @@
 ow.hour,
+%0A           
  'minute
@@ -4298,16 +4298,25 @@
 w.minute
+%0A        
 %7D%0A%0A    d

Migrate to voluptuous (#3276)