1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -9,71 +9,93 @@ import somecomfort
from homeassistant.components.climate import ClimateDevice, PLATFORM_SCHEMA
from homeassistant.components.climate.const import (
ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW,
FAN_AUTO, FAN_DIFFUSE, FAN_ON,
SUPPORT_AUX_HEAT, SUPPORT_FAN_MODE, SUPPORT_PRESET_MODE,
SUPPORT_TARGET_HUMIDITY, SUPPORT_TARGET_TEMPERATURE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
FAN_AUTO,
FAN_DIFFUSE,
FAN_ON,
SUPPORT_AUX_HEAT,
SUPPORT_FAN_MODE,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_HUMIDITY,
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE,
CURRENT_HVAC_COOL, CURRENT_HVAC_HEAT, CURRENT_HVAC_IDLE, CURRENT_HVAC_FAN,
HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL, HVAC_MODE_HEAT_COOL,
PRESET_AWAY, PRESET_NONE,
CURRENT_HVAC_COOL,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_IDLE,
CURRENT_HVAC_FAN,
HVAC_MODE_OFF,
HVAC_MODE_HEAT,
HVAC_MODE_COOL,
HVAC_MODE_HEAT_COOL,
PRESET_AWAY,
PRESET_NONE,
)
from homeassistant.const import (
CONF_PASSWORD, CONF_USERNAME, TEMP_CELSIUS, TEMP_FAHRENHEIT,
ATTR_TEMPERATURE, CONF_REGION)
CONF_PASSWORD,
CONF_USERNAME,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
ATTR_TEMPERATURE,
CONF_REGION,
)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
ATTR_FAN_ACTION = 'fan_action'
ATTR_FAN_ACTION = "fan_action"
CONF_COOL_AWAY_TEMPERATURE = 'away_cool_temperature'
CONF_HEAT_AWAY_TEMPERATURE = 'away_heat_temperature'
CONF_COOL_AWAY_TEMPERATURE = "away_cool_temperature"
CONF_HEAT_AWAY_TEMPERATURE = "away_heat_temperature"
DEFAULT_COOL_AWAY_TEMPERATURE = 88
DEFAULT_HEAT_AWAY_TEMPERATURE = 61
DEFAULT_REGION = 'eu'
REGIONS = ['eu', 'us']
DEFAULT_REGION = "eu"
REGIONS = ["eu", "us"]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_COOL_AWAY_TEMPERATURE,
default=DEFAULT_COOL_AWAY_TEMPERATURE): vol.Coerce(int),
vol.Optional(CONF_HEAT_AWAY_TEMPERATURE,
default=DEFAULT_HEAT_AWAY_TEMPERATURE): vol.Coerce(int),
vol.Optional(CONF_REGION, default=DEFAULT_REGION): vol.In(REGIONS),
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(
CONF_COOL_AWAY_TEMPERATURE, default=DEFAULT_COOL_AWAY_TEMPERATURE
): vol.Coerce(int),
vol.Optional(
CONF_HEAT_AWAY_TEMPERATURE, default=DEFAULT_HEAT_AWAY_TEMPERATURE
): vol.Coerce(int),
vol.Optional(CONF_REGION, default=DEFAULT_REGION): vol.In(REGIONS),
}
)
HVAC_MODE_TO_HW_MODE = {
'SwitchOffAllowed': {HVAC_MODE_OFF: 'off'},
'SwitchAutoAllowed': {HVAC_MODE_HEAT_COOL: 'auto'},
'SwitchCoolAllowed': {HVAC_MODE_COOL: 'cool'},
'SwitchHeatAllowed': {HVAC_MODE_HEAT: 'heat'},
"SwitchOffAllowed": {HVAC_MODE_OFF: "off"},
"SwitchAutoAllowed": {HVAC_MODE_HEAT_COOL: "auto"},
"SwitchCoolAllowed": {HVAC_MODE_COOL: "cool"},
"SwitchHeatAllowed": {HVAC_MODE_HEAT: "heat"},
}
HW_MODE_TO_HVAC_MODE = {
'off': HVAC_MODE_OFF,
'emheat': HVAC_MODE_HEAT,
'heat': HVAC_MODE_HEAT,
'cool': HVAC_MODE_COOL,
'auto': HVAC_MODE_HEAT_COOL,
"off": HVAC_MODE_OFF,
"emheat": HVAC_MODE_HEAT,
"heat": HVAC_MODE_HEAT,
"cool": HVAC_MODE_COOL,
"auto": HVAC_MODE_HEAT_COOL,
}
HW_MODE_TO_HA_HVAC_ACTION = {
'off': CURRENT_HVAC_IDLE,
'fan': CURRENT_HVAC_FAN,
'heat': CURRENT_HVAC_HEAT,
'cool': CURRENT_HVAC_COOL,
"off": CURRENT_HVAC_IDLE,
"fan": CURRENT_HVAC_FAN,
"heat": CURRENT_HVAC_HEAT,
"cool": CURRENT_HVAC_COOL,
}
FAN_MODE_TO_HW = {
'fanModeOnAllowed': {FAN_ON: 'on'},
'fanModeAutoAllowed': {FAN_AUTO: 'auto'},
'fanModeCirculateAllowed': {FAN_DIFFUSE: 'circulate'},
"fanModeOnAllowed": {FAN_ON: "on"},
"fanModeAutoAllowed": {FAN_AUTO: "auto"},
"fanModeCirculateAllowed": {FAN_DIFFUSE: "circulate"},
}
HW_FAN_MODE_TO_HA = {
'on': FAN_ON,
'auto': FAN_AUTO,
'circulate': FAN_DIFFUSE,
'follow schedule': FAN_AUTO,
"on": FAN_ON,
"auto": FAN_AUTO,
"circulate": FAN_DIFFUSE,
"follow schedule": FAN_AUTO,
}
@@ -82,42 +104,53 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
if config.get(CONF_REGION) == 'us':
if config.get(CONF_REGION) == "us":
try:
client = somecomfort.SomeComfort(username, password)
except somecomfort.AuthError:
_LOGGER.error("Failed to login to honeywell account %s", username)
return
except somecomfort.SomeComfortError:
_LOGGER.error("Failed to initialize the Honeywell client: "
"Check your configuration (username, password), "
"or maybe you have exceeded the API rate limit?")
_LOGGER.error(
"Failed to initialize the Honeywell client: "
"Check your configuration (username, password), "
"or maybe you have exceeded the API rate limit?"
)
return
dev_id = config.get('thermostat')
loc_id = config.get('location')
dev_id = config.get("thermostat")
loc_id = config.get("location")
cool_away_temp = config.get(CONF_COOL_AWAY_TEMPERATURE)
heat_away_temp = config.get(CONF_HEAT_AWAY_TEMPERATURE)
add_entities([HoneywellUSThermostat(client, device, cool_away_temp,
heat_away_temp, username, password)
for location in client.locations_by_id.values()
for device in location.devices_by_id.values()
if ((not loc_id or location.locationid == loc_id) and
(not dev_id or device.deviceid == dev_id))])
add_entities(
[
HoneywellUSThermostat(
client, device, cool_away_temp, heat_away_temp, username, password
)
for location in client.locations_by_id.values()
for device in location.devices_by_id.values()
if (
(not loc_id or location.locationid == loc_id)
and (not dev_id or device.deviceid == dev_id)
)
]
)
return
_LOGGER.warning(
"The honeywell component has been deprecated for EU (i.e. non-US) "
"systems. For EU-based systems, use the evohome component, "
"see: https://home-assistant.io/components/evohome")
"see: https://home-assistant.io/components/evohome"
)
class HoneywellUSThermostat(ClimateDevice):
"""Representation of a Honeywell US Thermostat."""
def __init__(self, client, device, cool_away_temp,
heat_away_temp, username, password):
def __init__(
self, client, device, cool_away_temp, heat_away_temp, username, password
):
"""Initialize the thermostat."""
self._client = client
self._device = device
@@ -127,30 +160,33 @@ class HoneywellUSThermostat(ClimateDevice):
self._username = username
self._password = password
_LOGGER.debug("latestData = %s ", device._data) # noqa; pylint: disable=protected-access
_LOGGER.debug(
"latestData = %s ", device._data
) # noqa; pylint: disable=protected-access
# not all honeywell HVACs support all modes
mappings = [v for k, v in HVAC_MODE_TO_HW_MODE.items()
if device.raw_ui_data[k]]
mappings = [v for k, v in HVAC_MODE_TO_HW_MODE.items() if device.raw_ui_data[k]]
self._hvac_mode_map = {k: v for d in mappings for k, v in d.items()}
self._supported_features = \
SUPPORT_PRESET_MODE | \
SUPPORT_TARGET_TEMPERATURE | \
SUPPORT_TARGET_TEMPERATURE_RANGE
self._supported_features = (
SUPPORT_PRESET_MODE
| SUPPORT_TARGET_TEMPERATURE
| SUPPORT_TARGET_TEMPERATURE_RANGE
)
if device._data['canControlHumidification']: # noqa; pylint: disable=protected-access
if device._data[
"canControlHumidification"
]: # noqa; pylint: disable=protected-access
self._supported_features |= SUPPORT_TARGET_HUMIDITY
if device.raw_ui_data['SwitchEmergencyHeatAllowed']:
if device.raw_ui_data["SwitchEmergencyHeatAllowed"]:
self._supported_features |= SUPPORT_AUX_HEAT
if not device._data['hasFan']: # pylint: disable=protected-access
if not device._data["hasFan"]: # pylint: disable=protected-access
return
# not all honeywell fans support all modes
mappings = [v for k, v in FAN_MODE_TO_HW.items()
if device.raw_fan_data[k]]
mappings = [v for k, v in FAN_MODE_TO_HW.items() if device.raw_fan_data[k]]
self._fan_mode_map = {k: v for d in mappings for k, v in d.items()}
self._supported_features |= SUPPORT_FAN_MODE
@@ -164,10 +200,9 @@ class HoneywellUSThermostat(ClimateDevice):
def device_state_attributes(self) -> Dict[str, Any]:
"""Return the device specific state attributes."""
data = {}
data[ATTR_FAN_ACTION] = \
'running' if self._device.fan_running else 'idle'
data[ATTR_FAN_ACTION] = "running" if self._device.fan_running else "idle"
if self._device.raw_dr_data:
data['dr_phase'] = self._device.raw_dr_data.get('Phase')
data["dr_phase"] = self._device.raw_dr_data.get("Phase")
return data
@property
@@ -179,25 +214,24 @@ class HoneywellUSThermostat(ClimateDevice):
def min_temp(self) -> float:
"""Return the minimum temperature."""
if self.hvac_mode in [HVAC_MODE_COOL, HVAC_MODE_HEAT_COOL]:
return self._device.raw_ui_data['CoolLowerSetptLimit']
return self._device.raw_ui_data["CoolLowerSetptLimit"]
if self.hvac_mode == HVAC_MODE_HEAT:
return self._device.raw_ui_data['HeatLowerSetptLimit']
return self._device.raw_ui_data["HeatLowerSetptLimit"]
return None
@property
def max_temp(self) -> float:
"""Return the maximum temperature."""
if self.hvac_mode == HVAC_MODE_COOL:
return self._device.raw_ui_data['CoolUpperSetptLimit']
return self._device.raw_ui_data["CoolUpperSetptLimit"]
if self.hvac_mode in [HVAC_MODE_HEAT, HVAC_MODE_HEAT_COOL]:
return self._device.raw_ui_data['HeatUpperSetptLimit']
return self._device.raw_ui_data["HeatUpperSetptLimit"]
return None
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
return (TEMP_CELSIUS if self._device.temperature_unit == 'C'
else TEMP_FAHRENHEIT)
return TEMP_CELSIUS if self._device.temperature_unit == "C" else TEMP_FAHRENHEIT
@property
def current_humidity(self) -> Optional[int]:
@@ -262,7 +296,7 @@ class HoneywellUSThermostat(ClimateDevice):
@property
def is_aux_heat(self) -> Optional[str]:
"""Return true if aux heater."""
return self._device.system_mode == 'emheat'
return self._device.system_mode == "emheat"
@property
def fan_mode(self) -> Optional[str]:
@@ -285,19 +319,17 @@ class HoneywellUSThermostat(ClimateDevice):
# Set hold if this is not the case
if getattr(self._device, "hold_{}".format(mode)) is False:
# Get next period key
next_period_key = '{}NextPeriod'.format(mode.capitalize())
next_period_key = "{}NextPeriod".format(mode.capitalize())
# Get next period raw value
next_period = self._device.raw_ui_data.get(next_period_key)
# Get next period time
hour, minute = divmod(next_period * 15, 60)
# Set hold time
setattr(self._device,
"hold_{}".format(mode),
datetime.time(hour, minute))
setattr(
self._device, "hold_{}".format(mode), datetime.time(hour, minute)
)
# Set temperature
setattr(self._device,
"setpoint_{}".format(mode),
temperature)
setattr(self._device, "setpoint_{}".format(mode), temperature)
except somecomfort.SomeComfortError:
_LOGGER.error("Temperature %.1f out of range", temperature)
@@ -337,21 +369,23 @@ class HoneywellUSThermostat(ClimateDevice):
# Get current mode
mode = self._device.system_mode
except somecomfort.SomeComfortError:
_LOGGER.error('Can not get system mode')
_LOGGER.error("Can not get system mode")
return
try:
# Set permanent hold
setattr(self._device,
"hold_{}".format(mode),
True)
setattr(self._device, "hold_{}".format(mode), True)
# Set temperature
setattr(self._device,
"setpoint_{}".format(mode),
getattr(self, "_{}_away_temp".format(mode)))
setattr(
self._device,
"setpoint_{}".format(mode),
getattr(self, "_{}_away_temp".format(mode)),
)
except somecomfort.SomeComfortError:
_LOGGER.error('Temperature %.1f out of range',
getattr(self, "_{}_away_temp".format(mode)))
_LOGGER.error(
"Temperature %.1f out of range",
getattr(self, "_{}_away_temp".format(mode)),
)
def _turn_away_mode_off(self) -> None:
"""Turn away off."""
@@ -361,7 +395,7 @@ class HoneywellUSThermostat(ClimateDevice):
self._device.hold_cool = False
self._device.hold_heat = False
except somecomfort.SomeComfortError:
_LOGGER.error('Can not stop hold mode')
_LOGGER.error("Can not stop hold mode")
def set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
@@ -372,7 +406,7 @@ class HoneywellUSThermostat(ClimateDevice):
def turn_aux_heat_on(self) -> None:
"""Turn auxiliary heater on."""
self._device.system_mode = 'emheat'
self._device.system_mode = "emheat"
def turn_aux_heat_off(self) -> None:
"""Turn auxiliary heater off."""
@@ -388,21 +422,20 @@ class HoneywellUSThermostat(ClimateDevice):
will succeed, is to recreate a new somecomfort client.
"""
try:
self._client = somecomfort.SomeComfort(
self._username, self._password)
self._client = somecomfort.SomeComfort(self._username, self._password)
except somecomfort.AuthError:
_LOGGER.error("Failed to login to honeywell account %s",
self._username)
_LOGGER.error("Failed to login to honeywell account %s", self._username)
return False
except somecomfort.SomeComfortError as ex:
_LOGGER.error("Failed to initialize honeywell client: %s",
str(ex))
_LOGGER.error("Failed to initialize honeywell client: %s", str(ex))
return False
devices = [device
for location in self._client.locations_by_id.values()
for device in location.devices_by_id.values()
if device.name == self._device.name]
devices = [
device
for location in self._client.locations_by_id.values()
for device in location.devices_by_id.values()
if device.name == self._device.name
]
if len(devices) != 1:
_LOGGER.error("Failed to find device %s", self._device.name)
@@ -418,14 +451,18 @@ class HoneywellUSThermostat(ClimateDevice):
try:
self._device.refresh()
break
except (somecomfort.client.APIRateLimited, OSError,
requests.exceptions.ReadTimeout) as exp:
except (
somecomfort.client.APIRateLimited,
OSError,
requests.exceptions.ReadTimeout,
) as exp:
retries -= 1
if retries == 0:
raise exp
if not self._retry():
raise exp
_LOGGER.error(
"SomeComfort update failed, Retrying - Error: %s", exp)
_LOGGER.error("SomeComfort update failed, Retrying - Error: %s", exp)
_LOGGER.debug("latestData = %s ", self._device._data) # noqa; pylint: disable=protected-access
_LOGGER.debug(
"latestData = %s ", self._device._data
) # noqa; pylint: disable=protected-access