"""The tests for the heat control thermostat."""
import unittest

from blumate.bootstrap import _setup_component
from blumate.const import (
    ATTR_UNIT_OF_MEASUREMENT,
    SERVICE_TURN_OFF,
    SERVICE_TURN_ON,
    STATE_ON,
    STATE_OFF,
    TEMP_CELSIUS,
)
from blumate.components import thermostat

from tests.common import get_test_home_assistant


ENTITY = 'thermostat.test'
ENT_SENSOR = 'sensor.test'
ENT_SWITCH = 'switch.test'
MIN_TEMP = 3.0
MAX_TEMP = 65.0
TARGET_TEMP = 42.0


class TestSetupThermostatHeatControl(unittest.TestCase):
    """Test the Heat Control thermostat with custom config."""

    def setUp(self):  # pylint: disable=invalid-name
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()

    def tearDown(self):  # pylint: disable=invalid-name
        """Stop down everything that was started."""
        self.hass.stop()

    def test_setup_missing_conf(self):
        """Test set up heat_control with missing config values."""
        config = {
            'name': 'test',
            'target_sensor': ENT_SENSOR
        }
        self.assertFalse(_setup_component(self.hass, 'thermostat', {
            'thermostat': config}))

    def test_valid_conf(self):
        """Test set up heat_control with valid config values."""
        self.assertTrue(_setup_component(self.hass, 'thermostat',
                        {'thermostat': {
                            'platform': 'heat_control',
                            'name': 'test',
                            'heater': ENT_SWITCH,
                            'target_sensor': ENT_SENSOR}}))

    def test_setup_with_sensor(self):
        """Test set up heat_control with sensor to trigger update at init."""
        self.hass.states.set(ENT_SENSOR, 22.0, {
            ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS
        })
        thermostat.setup(self.hass, {'thermostat': {
            'platform': 'heat_control',
            'name': 'test',
            'heater': ENT_SWITCH,
            'target_sensor': ENT_SENSOR
        }})
        state = self.hass.states.get(ENTITY)
        self.assertEqual(
            TEMP_CELSIUS, state.attributes.get('unit_of_measurement'))
        self.assertEqual(22.0, state.attributes.get('current_temperature'))