mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Black
This commit is contained in:
@@ -6,65 +6,86 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.components.climate import PLATFORM_SCHEMA, ClimateDevice
|
||||
from homeassistant.components.climate.const import (
|
||||
ATTR_PRESET_MODE, CURRENT_HVAC_COOL, CURRENT_HVAC_HEAT, CURRENT_HVAC_IDLE,
|
||||
CURRENT_HVAC_OFF, HVAC_MODE_COOL, HVAC_MODE_HEAT, HVAC_MODE_OFF,
|
||||
PRESET_AWAY, SUPPORT_PRESET_MODE, SUPPORT_TARGET_TEMPERATURE, PRESET_NONE)
|
||||
ATTR_PRESET_MODE,
|
||||
CURRENT_HVAC_COOL,
|
||||
CURRENT_HVAC_HEAT,
|
||||
CURRENT_HVAC_IDLE,
|
||||
CURRENT_HVAC_OFF,
|
||||
HVAC_MODE_COOL,
|
||||
HVAC_MODE_HEAT,
|
||||
HVAC_MODE_OFF,
|
||||
PRESET_AWAY,
|
||||
SUPPORT_PRESET_MODE,
|
||||
SUPPORT_TARGET_TEMPERATURE,
|
||||
PRESET_NONE,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, ATTR_TEMPERATURE, CONF_NAME, EVENT_HOMEASSISTANT_START,
|
||||
PRECISION_HALVES, PRECISION_TENTHS, PRECISION_WHOLE, SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON, STATE_ON, STATE_UNKNOWN)
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_TEMPERATURE,
|
||||
CONF_NAME,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
PRECISION_HALVES,
|
||||
PRECISION_TENTHS,
|
||||
PRECISION_WHOLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_ON,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import DOMAIN as HA_DOMAIN, callback
|
||||
from homeassistant.helpers import condition
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.event import (
|
||||
async_track_state_change, async_track_time_interval)
|
||||
async_track_state_change,
|
||||
async_track_time_interval,
|
||||
)
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_TOLERANCE = 0.3
|
||||
DEFAULT_NAME = 'Generic Thermostat'
|
||||
DEFAULT_NAME = "Generic Thermostat"
|
||||
|
||||
CONF_HEATER = 'heater'
|
||||
CONF_SENSOR = 'target_sensor'
|
||||
CONF_MIN_TEMP = 'min_temp'
|
||||
CONF_MAX_TEMP = 'max_temp'
|
||||
CONF_TARGET_TEMP = 'target_temp'
|
||||
CONF_AC_MODE = 'ac_mode'
|
||||
CONF_MIN_DUR = 'min_cycle_duration'
|
||||
CONF_COLD_TOLERANCE = 'cold_tolerance'
|
||||
CONF_HOT_TOLERANCE = 'hot_tolerance'
|
||||
CONF_KEEP_ALIVE = 'keep_alive'
|
||||
CONF_INITIAL_HVAC_MODE = 'initial_hvac_mode'
|
||||
CONF_AWAY_TEMP = 'away_temp'
|
||||
CONF_PRECISION = 'precision'
|
||||
CONF_HEATER = "heater"
|
||||
CONF_SENSOR = "target_sensor"
|
||||
CONF_MIN_TEMP = "min_temp"
|
||||
CONF_MAX_TEMP = "max_temp"
|
||||
CONF_TARGET_TEMP = "target_temp"
|
||||
CONF_AC_MODE = "ac_mode"
|
||||
CONF_MIN_DUR = "min_cycle_duration"
|
||||
CONF_COLD_TOLERANCE = "cold_tolerance"
|
||||
CONF_HOT_TOLERANCE = "hot_tolerance"
|
||||
CONF_KEEP_ALIVE = "keep_alive"
|
||||
CONF_INITIAL_HVAC_MODE = "initial_hvac_mode"
|
||||
CONF_AWAY_TEMP = "away_temp"
|
||||
CONF_PRECISION = "precision"
|
||||
SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_HEATER): cv.entity_id,
|
||||
vol.Required(CONF_SENSOR): cv.entity_id,
|
||||
vol.Optional(CONF_AC_MODE): cv.boolean,
|
||||
vol.Optional(CONF_MAX_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_MIN_DUR): vol.All(cv.time_period, cv.positive_timedelta),
|
||||
vol.Optional(CONF_MIN_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_COLD_TOLERANCE, default=DEFAULT_TOLERANCE): vol.Coerce(
|
||||
float),
|
||||
vol.Optional(CONF_HOT_TOLERANCE, default=DEFAULT_TOLERANCE): vol.Coerce(
|
||||
float),
|
||||
vol.Optional(CONF_TARGET_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_KEEP_ALIVE): vol.All(
|
||||
cv.time_period, cv.positive_timedelta),
|
||||
vol.Optional(CONF_INITIAL_HVAC_MODE):
|
||||
vol.In([HVAC_MODE_COOL, HVAC_MODE_HEAT, HVAC_MODE_OFF]),
|
||||
vol.Optional(CONF_AWAY_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_PRECISION): vol.In(
|
||||
[PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE]),
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_HEATER): cv.entity_id,
|
||||
vol.Required(CONF_SENSOR): cv.entity_id,
|
||||
vol.Optional(CONF_AC_MODE): cv.boolean,
|
||||
vol.Optional(CONF_MAX_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_MIN_DUR): vol.All(cv.time_period, cv.positive_timedelta),
|
||||
vol.Optional(CONF_MIN_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_COLD_TOLERANCE, default=DEFAULT_TOLERANCE): vol.Coerce(float),
|
||||
vol.Optional(CONF_HOT_TOLERANCE, default=DEFAULT_TOLERANCE): vol.Coerce(float),
|
||||
vol.Optional(CONF_TARGET_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_KEEP_ALIVE): vol.All(cv.time_period, cv.positive_timedelta),
|
||||
vol.Optional(CONF_INITIAL_HVAC_MODE): vol.In(
|
||||
[HVAC_MODE_COOL, HVAC_MODE_HEAT, HVAC_MODE_OFF]
|
||||
),
|
||||
vol.Optional(CONF_AWAY_TEMP): vol.Coerce(float),
|
||||
vol.Optional(CONF_PRECISION): vol.In(
|
||||
[PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE]
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the generic thermostat platform."""
|
||||
name = config.get(CONF_NAME)
|
||||
heater_entity_id = config.get(CONF_HEATER)
|
||||
@@ -82,20 +103,50 @@ async def async_setup_platform(hass, config, async_add_entities,
|
||||
precision = config.get(CONF_PRECISION)
|
||||
unit = hass.config.units.temperature_unit
|
||||
|
||||
async_add_entities([GenericThermostat(
|
||||
name, heater_entity_id, sensor_entity_id, min_temp, max_temp,
|
||||
target_temp, ac_mode, min_cycle_duration, cold_tolerance,
|
||||
hot_tolerance, keep_alive, initial_hvac_mode, away_temp,
|
||||
precision, unit)])
|
||||
async_add_entities(
|
||||
[
|
||||
GenericThermostat(
|
||||
name,
|
||||
heater_entity_id,
|
||||
sensor_entity_id,
|
||||
min_temp,
|
||||
max_temp,
|
||||
target_temp,
|
||||
ac_mode,
|
||||
min_cycle_duration,
|
||||
cold_tolerance,
|
||||
hot_tolerance,
|
||||
keep_alive,
|
||||
initial_hvac_mode,
|
||||
away_temp,
|
||||
precision,
|
||||
unit,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
"""Representation of a Generic Thermostat device."""
|
||||
|
||||
def __init__(self, name, heater_entity_id, sensor_entity_id,
|
||||
min_temp, max_temp, target_temp, ac_mode, min_cycle_duration,
|
||||
cold_tolerance, hot_tolerance, keep_alive,
|
||||
initial_hvac_mode, away_temp, precision, unit):
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
heater_entity_id,
|
||||
sensor_entity_id,
|
||||
min_temp,
|
||||
max_temp,
|
||||
target_temp,
|
||||
ac_mode,
|
||||
min_cycle_duration,
|
||||
cold_tolerance,
|
||||
hot_tolerance,
|
||||
keep_alive,
|
||||
initial_hvac_mode,
|
||||
away_temp,
|
||||
precision,
|
||||
unit,
|
||||
):
|
||||
"""Initialize the thermostat."""
|
||||
self._name = name
|
||||
self.heater_entity_id = heater_entity_id
|
||||
@@ -131,13 +182,16 @@ class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
|
||||
# Add listener
|
||||
async_track_state_change(
|
||||
self.hass, self.sensor_entity_id, self._async_sensor_changed)
|
||||
self.hass, self.sensor_entity_id, self._async_sensor_changed
|
||||
)
|
||||
async_track_state_change(
|
||||
self.hass, self.heater_entity_id, self._async_switch_changed)
|
||||
self.hass, self.heater_entity_id, self._async_switch_changed
|
||||
)
|
||||
|
||||
if self._keep_alive:
|
||||
async_track_time_interval(
|
||||
self.hass, self._async_control_heating, self._keep_alive)
|
||||
self.hass, self._async_control_heating, self._keep_alive
|
||||
)
|
||||
|
||||
@callback
|
||||
def _async_startup(event):
|
||||
@@ -146,8 +200,7 @@ class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
if sensor_state and sensor_state.state != STATE_UNKNOWN:
|
||||
self._async_update_temp(sensor_state)
|
||||
|
||||
self.hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_START, _async_startup)
|
||||
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
|
||||
|
||||
# Check If we have an old state
|
||||
old_state = await self.async_get_last_state()
|
||||
@@ -160,11 +213,12 @@ class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
self._target_temp = self.max_temp
|
||||
else:
|
||||
self._target_temp = self.min_temp
|
||||
_LOGGER.warning("Undefined target temperature,"
|
||||
"falling back to %s", self._target_temp)
|
||||
_LOGGER.warning(
|
||||
"Undefined target temperature," "falling back to %s",
|
||||
self._target_temp,
|
||||
)
|
||||
else:
|
||||
self._target_temp = float(
|
||||
old_state.attributes[ATTR_TEMPERATURE])
|
||||
self._target_temp = float(old_state.attributes[ATTR_TEMPERATURE])
|
||||
if old_state.attributes.get(ATTR_PRESET_MODE) == PRESET_AWAY:
|
||||
self._is_away = True
|
||||
if not self._hvac_mode and old_state.state:
|
||||
@@ -177,8 +231,9 @@ class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
self._target_temp = self.max_temp
|
||||
else:
|
||||
self._target_temp = self.min_temp
|
||||
_LOGGER.warning("No previously saved temperature, setting to %s",
|
||||
self._target_temp)
|
||||
_LOGGER.warning(
|
||||
"No previously saved temperature, setting to %s", self._target_temp
|
||||
)
|
||||
|
||||
# Set default state to off
|
||||
if not self._hvac_mode:
|
||||
@@ -326,12 +381,14 @@ class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
async def _async_control_heating(self, time=None, force=False):
|
||||
"""Check if we need to turn heating on or off."""
|
||||
async with self._temp_lock:
|
||||
if not self._active and None not in (self._cur_temp,
|
||||
self._target_temp):
|
||||
if not self._active and None not in (self._cur_temp, self._target_temp):
|
||||
self._active = True
|
||||
_LOGGER.info("Obtained current and target temperature. "
|
||||
"Generic thermostat active. %s, %s",
|
||||
self._cur_temp, self._target_temp)
|
||||
_LOGGER.info(
|
||||
"Obtained current and target temperature. "
|
||||
"Generic thermostat active. %s, %s",
|
||||
self._cur_temp,
|
||||
self._target_temp,
|
||||
)
|
||||
|
||||
if not self._active or self._hvac_mode == HVAC_MODE_OFF:
|
||||
return
|
||||
@@ -347,27 +404,25 @@ class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
else:
|
||||
current_state = HVAC_MODE_OFF
|
||||
long_enough = condition.state(
|
||||
self.hass, self.heater_entity_id, current_state,
|
||||
self.min_cycle_duration)
|
||||
self.hass,
|
||||
self.heater_entity_id,
|
||||
current_state,
|
||||
self.min_cycle_duration,
|
||||
)
|
||||
if not long_enough:
|
||||
return
|
||||
|
||||
too_cold = \
|
||||
self._target_temp - self._cur_temp >= self._cold_tolerance
|
||||
too_hot = \
|
||||
self._cur_temp - self._target_temp >= self._hot_tolerance
|
||||
too_cold = self._target_temp - self._cur_temp >= self._cold_tolerance
|
||||
too_hot = self._cur_temp - self._target_temp >= self._hot_tolerance
|
||||
if self._is_device_active:
|
||||
if (self.ac_mode and too_cold) or \
|
||||
(not self.ac_mode and too_hot):
|
||||
_LOGGER.info("Turning off heater %s",
|
||||
self.heater_entity_id)
|
||||
if (self.ac_mode and too_cold) or (not self.ac_mode and too_hot):
|
||||
_LOGGER.info("Turning off heater %s", self.heater_entity_id)
|
||||
await self._async_heater_turn_off()
|
||||
elif time is not None:
|
||||
# The time argument is passed only in keep-alive case
|
||||
await self._async_heater_turn_on()
|
||||
else:
|
||||
if (self.ac_mode and too_hot) or \
|
||||
(not self.ac_mode and too_cold):
|
||||
if (self.ac_mode and too_hot) or (not self.ac_mode and too_cold):
|
||||
_LOGGER.info("Turning on heater %s", self.heater_entity_id)
|
||||
await self._async_heater_turn_on()
|
||||
elif time is not None:
|
||||
|
||||
Reference in New Issue
Block a user