hass-core/homeassistant/components/mqtt/climate.py
Pascal Vizeli 84cf76ba36
Climate 1.0 (#23899)
* Climate 1.0 / part 1/2/3

* fix flake

* Lint

* Update Google Assistant

* ambiclimate to climate 1.0 (#24911)

* Fix Alexa

* Lint

* Migrate zhong_hong

* Migrate tuya

* Migrate honeywell to new climate schema (#24257)

* Update one

* Fix model climate v2

* Cleanup p4

* Add comfort hold mode

* Fix old code

* Update homeassistant/components/climate/__init__.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* Update homeassistant/components/climate/const.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* First renaming

* Rename operation to hvac for paulus

* Rename hold mode to preset mode

* Cleanup & update comments

* Remove on/off

* Fix supported feature count

* Update services

* Update demo

* Fix tests & use current_hvac

* Update comment

* Fix tests & add typing

* Add more typing

* Update modes

* Fix tests

* Cleanup low/high with range

* Update homematic part 1

* Finish homematic

* Fix lint

* fix hm mapping

* Support simple devices

* convert lcn

* migrate oem

* Fix xs1

* update hive

* update mil

* Update toon

* migrate deconz

* cleanup

* update tesla

* Fix lint

* Fix vera

* Migrate zwave

* Migrate velbus

* Cleanup humity feature

* Cleanup

* Migrate wink

* migrate dyson

* Fix current hvac

* Renaming

* Fix lint

* Migrate tfiac

* migrate tado

* Fix PRESET can be None

* apply PR#23913 from dev

* remove EU component, etc.

* remove EU component, etc.

* ready to test now

* de-linted

* some tweaks

* de-lint

* better handling of edge cases

* delint

* fix set_mode typos

* apply PR#23913 from dev

* remove EU component, etc.

* ready to test now

* de-linted

* some tweaks

* de-lint

* better handling of edge cases

* delint

* fix set_mode typos

* delint, move debug code

* away preset now working

* code tidy-up

* code tidy-up 2

* code tidy-up 3

* address issues #18932, #15063

* address issues #18932, #15063 - 2/2

* refactor MODE_AUTO to MODE_HEAT_COOL and use F not C

* add low/high to set_temp

* add low/high to set_temp 2

* add low/high to set_temp - delint

* run HA scripts

* port changes from PR #24402

* manual rebase

* manual rebase 2

* delint

* minor change

* remove SUPPORT_HVAC_ACTION

* Migrate radiotherm

* Convert touchline

* Migrate flexit

* Migrate nuheat

* Migrate maxcube

* Fix names maxcube const

* Migrate proliphix

* Migrate heatmiser

* Migrate fritzbox

* Migrate opentherm_gw

* Migrate venstar

* Migrate daikin

* Migrate modbus

* Fix elif

* Migrate Homematic IP Cloud to climate-1.0 (#24913)

* hmip climate fix

* Update hvac_mode and preset_mode

* fix lint

* Fix lint

* Migrate generic_thermostat

* Migrate incomfort to new climate schema (#24915)

* initial commit

* Update climate.py

* Migrate eq3btsmart

* Lint

* cleanup PRESET_MANUAL

* Migrate ecobee

* No conditional features

* KNX: Migrate climate component to new climate platform (#24931)

* Migrate climate component

* Remove unused code

* Corrected line length

* Lint

* Lint

* fix tests

* Fix value

* Migrate geniushub to new climate schema (#24191)

* Update one

* Fix model climate v2

* Cleanup p4

* Add comfort hold mode

* Fix old code

* Update homeassistant/components/climate/__init__.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* Update homeassistant/components/climate/const.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* First renaming

* Rename operation to hvac for paulus

* Rename hold mode to preset mode

* Cleanup & update comments

* Remove on/off

* Fix supported feature count

* Update services

* Update demo

* Fix tests & use current_hvac

* Update comment

* Fix tests & add typing

* Add more typing

* Update modes

* Fix tests

* Cleanup low/high with range

* Update homematic part 1

* Finish homematic

* Fix lint

* fix hm mapping

* Support simple devices

* convert lcn

* migrate oem

* Fix xs1

* update hive

* update mil

* Update toon

* migrate deconz

* cleanup

* update tesla

* Fix lint

* Fix vera

* Migrate zwave

* Migrate velbus

* Cleanup humity feature

* Cleanup

* Migrate wink

* migrate dyson

* Fix current hvac

* Renaming

* Fix lint

* Migrate tfiac

* migrate tado

* delinted

* delinted

* use latest client

* clean up mappings

* clean up mappings

* add duration to set_temperature

* add duration to set_temperature

* manual rebase

* tweak

* fix regression

* small fix

* fix rebase mixup

* address comments

* finish refactor

* fix regression

* tweak type hints

* delint

* manual rebase

* WIP: Fixes for honeywell migration to climate-1.0 (#24938)

* add type hints

* code tidy-up

* Fixes for incomfort migration to climate-1.0 (#24936)

* delint type hints

* no async unless await

* revert: no async unless await

* revert: no async unless await 2

* delint

* fix typo

* Fix homekit_controller on climate-1.0 (#24948)

* Fix tests on climate-1.0 branch

* As part of climate-1.0, make state return the heating-cooling.current characteristic

* Fixes from review

* lint

* Fix imports

* Migrate stibel_eltron

* Fix lint

* Migrate coolmaster to climate 1.0 (#24967)

* Migrate coolmaster to climate 1.0

* fix lint errors

* More lint fixes

* Fix demo to work with UI

* Migrate spider

* Demo update

* Updated frontend to 20190705.0

* Fix boost mode (#24980)

* Prepare Netatmo for climate 1.0 (#24973)

* Migration Netatmo

* Address comments

* Update climate.py

* Migrate ephember

* Migrate Sensibo

* Implemented review comments (#24942)

* Migrate ESPHome

* Migrate MQTT

* Migrate Nest

* Migrate melissa

* Initial/partial migration of ST

* Migrate ST

* Remove Away mode (#24995)

* Migrate evohome, cache access tokens (#24491)

* add water_heater, add storage - initial commit

* add water_heater, add storage - initial commit

delint

add missing code

desiderata

update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

delint

* Add Broker, Water Heater & Refactor

add missing code

desiderata

* update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

* bugfix - loc_idx may not be 0

more refactor - ensure pure async

more refactoring

appears all r/o attributes are working

tweak precsion, DHW & delint

remove unused code

remove unused code 2

remove unused code, refactor _save_auth_tokens()

* support RoundThermostat

bugfix opmode, switch to util.dt, add until=1h

revert breaking change

* store at_expires as naive UTC

remove debug code

delint

tidy up exception handling

delint

add water_heater, add storage - initial commit

delint

add missing code

desiderata

update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

add water_heater, add storage - initial commit

delint

add missing code

desiderata

update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

delint

bugfix - loc_idx may not be 0

more refactor - ensure pure async

more refactoring

appears all r/o attributes are working

tweak precsion, DHW & delint

remove unused code

remove unused code 2

remove unused code, refactor _save_auth_tokens()

support RoundThermostat

bugfix opmode, switch to util.dt, add until=1h

revert breaking change

store at_expires as naive UTC

remove debug code

delint

tidy up exception handling

delint

* update CODEOWNERS

* fix regression

* fix requirements

* migrate to climate-1.0

* tweaking

* de-lint

* TCS working? & delint

* tweaking

* TCS code finalised

* remove available() logic

* refactor _switchpoints()

* tidy up switchpoint code

* tweak

* teaking device_state_attributes

* some refactoring

* move PRESET_CUSTOM back to evohome

* move CONF_ACCESS_TOKEN_EXPIRES CONF_REFRESH_TOKEN back to evohome

* refactor SP code and dt conversion

* delinted

* delinted

* remove water_heater

* fix regression

* Migrate homekit

* Cleanup away mode

* Fix tests

* add helpers

* fix tests melissa

* Fix nehueat

* fix zwave

* add more tests

* fix deconz

* Fix climate test emulate_hue

* fix tests

* fix dyson tests

* fix demo with new layout

* fix honeywell

* Switch homekit_controller to use HVAC_MODE_HEAT_COOL instead of HVAC_MODE_AUTO (#25009)

* Lint

* PyLint

* Pylint

* fix fritzbox tests

* Fix google

* Fix all tests

* Fix lint

* Fix auto for homekit like controler

* Fix lint

* fix lint
2019-07-08 14:00:24 +02:00

757 lines
28 KiB
Python

"""Support for MQTT climate devices."""
import logging
import voluptuous as vol
from homeassistant.components import climate, mqtt
from homeassistant.components.climate import (
PLATFORM_SCHEMA as CLIMATE_PLATFORM_SCHEMA, ClimateDevice)
from homeassistant.components.climate.const import (
ATTR_HVAC_MODE, ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW,
DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, HVAC_MODE_AUTO, HVAC_MODE_COOL,
HVAC_MODE_DRY, HVAC_MODE_FAN_ONLY, HVAC_MODE_HEAT, HVAC_MODE_OFF,
SUPPORT_AUX_HEAT, SUPPORT_FAN_MODE, SUPPORT_PRESET_MODE,
SUPPORT_SWING_MODE, SUPPORT_TARGET_TEMPERATURE, PRESET_AWAY,
SUPPORT_TARGET_TEMPERATURE_RANGE)
from homeassistant.components.fan import SPEED_HIGH, SPEED_LOW, SPEED_MEDIUM
from homeassistant.const import (
ATTR_TEMPERATURE, CONF_DEVICE, CONF_NAME, CONF_VALUE_TEMPLATE, STATE_ON)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from . import (
ATTR_DISCOVERY_HASH, CONF_QOS, CONF_RETAIN, CONF_UNIQUE_ID,
MQTT_BASE_PLATFORM_SCHEMA, MqttAttributes, MqttAvailability,
MqttDiscoveryUpdate, MqttEntityDeviceInfo, subscription)
from .discovery import MQTT_DISCOVERY_NEW, clear_discovery_hash
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'MQTT HVAC'
CONF_AUX_COMMAND_TOPIC = 'aux_command_topic'
CONF_AUX_STATE_TEMPLATE = 'aux_state_template'
CONF_AUX_STATE_TOPIC = 'aux_state_topic'
CONF_AWAY_MODE_COMMAND_TOPIC = 'away_mode_command_topic'
CONF_AWAY_MODE_STATE_TEMPLATE = 'away_mode_state_template'
CONF_AWAY_MODE_STATE_TOPIC = 'away_mode_state_topic'
CONF_CURRENT_TEMP_TEMPLATE = 'current_temperature_template'
CONF_CURRENT_TEMP_TOPIC = 'current_temperature_topic'
CONF_FAN_MODE_COMMAND_TOPIC = 'fan_mode_command_topic'
CONF_FAN_MODE_LIST = 'fan_modes'
CONF_FAN_MODE_STATE_TEMPLATE = 'fan_mode_state_template'
CONF_FAN_MODE_STATE_TOPIC = 'fan_mode_state_topic'
CONF_HOLD_COMMAND_TOPIC = 'hold_command_topic'
CONF_HOLD_STATE_TEMPLATE = 'hold_state_template'
CONF_HOLD_STATE_TOPIC = 'hold_state_topic'
CONF_HOLD_LIST = 'hold_modes'
CONF_MODE_COMMAND_TOPIC = 'mode_command_topic'
CONF_MODE_LIST = 'modes'
CONF_MODE_STATE_TEMPLATE = 'mode_state_template'
CONF_MODE_STATE_TOPIC = 'mode_state_topic'
CONF_PAYLOAD_OFF = 'payload_off'
CONF_PAYLOAD_ON = 'payload_on'
CONF_POWER_COMMAND_TOPIC = 'power_command_topic'
CONF_POWER_STATE_TEMPLATE = 'power_state_template'
CONF_POWER_STATE_TOPIC = 'power_state_topic'
CONF_SEND_IF_OFF = 'send_if_off'
CONF_SWING_MODE_COMMAND_TOPIC = 'swing_mode_command_topic'
CONF_SWING_MODE_LIST = 'swing_modes'
CONF_SWING_MODE_STATE_TEMPLATE = 'swing_mode_state_template'
CONF_SWING_MODE_STATE_TOPIC = 'swing_mode_state_topic'
CONF_TEMP_COMMAND_TOPIC = 'temperature_command_topic'
CONF_TEMP_HIGH_COMMAND_TOPIC = 'temperature_high_command_topic'
CONF_TEMP_HIGH_STATE_TEMPLATE = 'temperature_high_state_template'
CONF_TEMP_HIGH_STATE_TOPIC = 'temperature_high_state_topic'
CONF_TEMP_LOW_COMMAND_TOPIC = 'temperature_low_command_topic'
CONF_TEMP_LOW_STATE_TEMPLATE = 'temperature_low_state_template'
CONF_TEMP_LOW_STATE_TOPIC = 'temperature_low_state_topic'
CONF_TEMP_STATE_TEMPLATE = 'temperature_state_template'
CONF_TEMP_STATE_TOPIC = 'temperature_state_topic'
CONF_TEMP_INITIAL = 'initial'
CONF_TEMP_MAX = 'max_temp'
CONF_TEMP_MIN = 'min_temp'
CONF_TEMP_STEP = 'temp_step'
TEMPLATE_KEYS = (
CONF_AUX_STATE_TEMPLATE,
CONF_AWAY_MODE_STATE_TEMPLATE,
CONF_CURRENT_TEMP_TEMPLATE,
CONF_FAN_MODE_STATE_TEMPLATE,
CONF_HOLD_STATE_TEMPLATE,
CONF_MODE_STATE_TEMPLATE,
CONF_POWER_STATE_TEMPLATE,
CONF_SWING_MODE_STATE_TEMPLATE,
CONF_TEMP_HIGH_STATE_TEMPLATE,
CONF_TEMP_LOW_STATE_TEMPLATE,
CONF_TEMP_STATE_TEMPLATE,
)
TOPIC_KEYS = (
CONF_AUX_COMMAND_TOPIC,
CONF_AUX_STATE_TOPIC,
CONF_AWAY_MODE_COMMAND_TOPIC,
CONF_AWAY_MODE_STATE_TOPIC,
CONF_CURRENT_TEMP_TOPIC,
CONF_FAN_MODE_COMMAND_TOPIC,
CONF_FAN_MODE_STATE_TOPIC,
CONF_HOLD_COMMAND_TOPIC,
CONF_HOLD_STATE_TOPIC,
CONF_MODE_COMMAND_TOPIC,
CONF_MODE_STATE_TOPIC,
CONF_POWER_COMMAND_TOPIC,
CONF_POWER_STATE_TOPIC,
CONF_SWING_MODE_COMMAND_TOPIC,
CONF_SWING_MODE_STATE_TOPIC,
CONF_TEMP_COMMAND_TOPIC,
CONF_TEMP_HIGH_COMMAND_TOPIC,
CONF_TEMP_HIGH_STATE_TOPIC,
CONF_TEMP_LOW_COMMAND_TOPIC,
CONF_TEMP_LOW_STATE_TOPIC,
CONF_TEMP_STATE_TOPIC,
)
SCHEMA_BASE = CLIMATE_PLATFORM_SCHEMA.extend(MQTT_BASE_PLATFORM_SCHEMA.schema)
PLATFORM_SCHEMA = SCHEMA_BASE.extend({
vol.Optional(CONF_AUX_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_AUX_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_AUX_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_AWAY_MODE_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_AWAY_MODE_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_AWAY_MODE_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_CURRENT_TEMP_TEMPLATE): cv.template,
vol.Optional(CONF_CURRENT_TEMP_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
vol.Optional(CONF_FAN_MODE_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_FAN_MODE_LIST,
default=[HVAC_MODE_AUTO, SPEED_LOW,
SPEED_MEDIUM, SPEED_HIGH]): cv.ensure_list,
vol.Optional(CONF_FAN_MODE_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_FAN_MODE_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_HOLD_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_HOLD_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_HOLD_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_HOLD_LIST, default=list): cv.ensure_list,
vol.Optional(CONF_MODE_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_MODE_LIST,
default=[HVAC_MODE_AUTO, HVAC_MODE_OFF, HVAC_MODE_COOL,
HVAC_MODE_HEAT,
HVAC_MODE_DRY, HVAC_MODE_FAN_ONLY]): cv.ensure_list,
vol.Optional(CONF_MODE_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_MODE_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PAYLOAD_ON, default="ON"): cv.string,
vol.Optional(CONF_PAYLOAD_OFF, default="OFF"): cv.string,
vol.Optional(CONF_POWER_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_POWER_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_POWER_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_RETAIN, default=mqtt.DEFAULT_RETAIN): cv.boolean,
vol.Optional(CONF_SEND_IF_OFF, default=True): cv.boolean,
vol.Optional(CONF_SWING_MODE_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_SWING_MODE_LIST,
default=[STATE_ON, HVAC_MODE_OFF]): cv.ensure_list,
vol.Optional(CONF_SWING_MODE_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_SWING_MODE_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_TEMP_INITIAL, default=21): cv.positive_int,
vol.Optional(CONF_TEMP_MIN, default=DEFAULT_MIN_TEMP): vol.Coerce(float),
vol.Optional(CONF_TEMP_MAX, default=DEFAULT_MAX_TEMP): vol.Coerce(float),
vol.Optional(CONF_TEMP_STEP, default=1.0): vol.Coerce(float),
vol.Optional(CONF_TEMP_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_TEMP_HIGH_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_TEMP_HIGH_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_TEMP_LOW_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_TEMP_LOW_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_TEMP_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_TEMP_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
async_add_entities, discovery_info=None):
"""Set up MQTT climate device through configuration.yaml."""
await _async_setup_entity(hass, config, async_add_entities)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up MQTT climate device dynamically through MQTT discovery."""
async def async_discover(discovery_payload):
"""Discover and add a MQTT climate device."""
try:
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(hass, config, async_add_entities,
config_entry, discovery_hash)
except Exception:
if discovery_hash:
clear_discovery_hash(hass, discovery_hash)
raise
async_dispatcher_connect(
hass, MQTT_DISCOVERY_NEW.format(climate.DOMAIN, 'mqtt'),
async_discover)
async def _async_setup_entity(hass, config, async_add_entities,
config_entry=None, discovery_hash=None):
"""Set up the MQTT climate devices."""
async_add_entities([MqttClimate(hass, config, config_entry,
discovery_hash,)])
class MqttClimate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
MqttEntityDeviceInfo, ClimateDevice):
"""Representation of an MQTT climate device."""
def __init__(self, hass, config, config_entry, discovery_hash):
"""Initialize the climate device."""
self._config = config
self._unique_id = config.get(CONF_UNIQUE_ID)
self._sub_state = None
self.hass = hass
self._aux = False
self._away = False
self._current_fan_mode = None
self._current_operation = None
self._current_swing_mode = None
self._current_temp = None
self._hold = None
self._target_temp = None
self._target_temp_high = None
self._target_temp_low = None
self._topic = None
self._unit_of_measurement = hass.config.units.temperature_unit
self._value_templates = None
self._setup_from_config(config)
device_config = config.get(CONF_DEVICE)
MqttAttributes.__init__(self, config)
MqttAvailability.__init__(self, config)
MqttDiscoveryUpdate.__init__(self, discovery_hash,
self.discovery_update)
MqttEntityDeviceInfo.__init__(self, device_config, config_entry)
async def async_added_to_hass(self):
"""Handle being added to home assistant."""
await super().async_added_to_hass()
await self._subscribe_topics()
async def discovery_update(self, discovery_payload):
"""Handle updated discovery message."""
config = PLATFORM_SCHEMA(discovery_payload)
self._config = config
self._setup_from_config(config)
await self.attributes_discovery_update(config)
await self.availability_discovery_update(config)
await self.device_info_discovery_update(config)
await self._subscribe_topics()
self.async_write_ha_state()
def _setup_from_config(self, config):
"""(Re)Setup the entity."""
self._topic = {
key: config.get(key) for key in TOPIC_KEYS
}
# set to None in non-optimistic mode
self._target_temp = self._current_fan_mode = \
self._current_operation = self._current_swing_mode = None
self._target_temp_low = None
self._target_temp_high = None
if self._topic[CONF_TEMP_STATE_TOPIC] is None:
self._target_temp = config[CONF_TEMP_INITIAL]
if self._topic[CONF_TEMP_LOW_STATE_TOPIC] is None:
self._target_temp_low = config[CONF_TEMP_INITIAL]
if self._topic[CONF_TEMP_HIGH_STATE_TOPIC] is None:
self._target_temp_high = config[CONF_TEMP_INITIAL]
if self._topic[CONF_FAN_MODE_STATE_TOPIC] is None:
self._current_fan_mode = SPEED_LOW
if self._topic[CONF_SWING_MODE_STATE_TOPIC] is None:
self._current_swing_mode = HVAC_MODE_OFF
if self._topic[CONF_MODE_STATE_TOPIC] is None:
self._current_operation = HVAC_MODE_OFF
self._away = False
self._hold = None
self._aux = False
value_templates = {}
for key in TEMPLATE_KEYS:
value_templates[key] = lambda value: value
if CONF_VALUE_TEMPLATE in config:
value_template = config.get(CONF_VALUE_TEMPLATE)
value_template.hass = self.hass
value_templates = {
key: value_template.async_render_with_possible_json_value
for key in TEMPLATE_KEYS}
for key in TEMPLATE_KEYS & config.keys():
tpl = config[key]
value_templates[key] = tpl.async_render_with_possible_json_value
tpl.hass = self.hass
self._value_templates = value_templates
async def _subscribe_topics(self):
"""(Re)Subscribe to topics."""
topics = {}
qos = self._config[CONF_QOS]
def add_subscription(topics, topic, msg_callback):
if self._topic[topic] is not None:
topics[topic] = {
'topic': self._topic[topic],
'msg_callback': msg_callback,
'qos': qos}
def render_template(msg, template_name):
template = self._value_templates[template_name]
return template(msg.payload)
@callback
def handle_temperature_received(msg, template_name, attr):
"""Handle temperature coming via MQTT."""
payload = render_template(msg, template_name)
try:
setattr(self, attr, float(payload))
self.async_write_ha_state()
except ValueError:
_LOGGER.error("Could not parse temperature from %s", payload)
@callback
def handle_current_temperature_received(msg):
"""Handle current temperature coming via MQTT."""
handle_temperature_received(
msg, CONF_CURRENT_TEMP_TEMPLATE, '_current_temp')
add_subscription(topics, CONF_CURRENT_TEMP_TOPIC,
handle_current_temperature_received)
@callback
def handle_target_temperature_received(msg):
"""Handle target temperature coming via MQTT."""
handle_temperature_received(
msg, CONF_TEMP_STATE_TEMPLATE, '_target_temp')
add_subscription(topics, CONF_TEMP_STATE_TOPIC,
handle_target_temperature_received)
@callback
def handle_temperature_low_received(msg):
"""Handle target temperature low coming via MQTT."""
handle_temperature_received(
msg, CONF_TEMP_LOW_STATE_TEMPLATE, '_target_temp_low')
add_subscription(topics, CONF_TEMP_LOW_STATE_TOPIC,
handle_temperature_low_received)
@callback
def handle_temperature_high_received(msg):
"""Handle target temperature high coming via MQTT."""
handle_temperature_received(
msg, CONF_TEMP_HIGH_STATE_TEMPLATE, '_target_temp_high')
add_subscription(topics, CONF_TEMP_HIGH_STATE_TOPIC,
handle_temperature_high_received)
@callback
def handle_mode_received(msg, template_name, attr, mode_list):
"""Handle receiving listed mode via MQTT."""
payload = render_template(msg, template_name)
if payload not in self._config[mode_list]:
_LOGGER.error("Invalid %s mode: %s", mode_list, payload)
else:
setattr(self, attr, payload)
self.async_write_ha_state()
@callback
def handle_current_mode_received(msg):
"""Handle receiving mode via MQTT."""
handle_mode_received(msg, CONF_MODE_STATE_TEMPLATE,
'_current_operation', CONF_MODE_LIST)
add_subscription(topics, CONF_MODE_STATE_TOPIC,
handle_current_mode_received)
@callback
def handle_fan_mode_received(msg):
"""Handle receiving fan mode via MQTT."""
handle_mode_received(msg, CONF_FAN_MODE_STATE_TEMPLATE,
'_current_fan_mode', CONF_FAN_MODE_LIST)
add_subscription(topics, CONF_FAN_MODE_STATE_TOPIC,
handle_fan_mode_received)
@callback
def handle_swing_mode_received(msg):
"""Handle receiving swing mode via MQTT."""
handle_mode_received(msg, CONF_SWING_MODE_STATE_TEMPLATE,
'_current_swing_mode', CONF_SWING_MODE_LIST)
add_subscription(topics, CONF_SWING_MODE_STATE_TOPIC,
handle_swing_mode_received)
@callback
def handle_onoff_mode_received(msg, template_name, attr):
"""Handle receiving on/off mode via MQTT."""
payload = render_template(msg, template_name)
payload_on = self._config[CONF_PAYLOAD_ON]
payload_off = self._config[CONF_PAYLOAD_OFF]
if payload == "True":
payload = payload_on
elif payload == "False":
payload = payload_off
if payload == payload_on:
setattr(self, attr, True)
elif payload == payload_off:
setattr(self, attr, False)
else:
_LOGGER.error("Invalid %s mode: %s", attr, payload)
self.async_write_ha_state()
@callback
def handle_away_mode_received(msg):
"""Handle receiving away mode via MQTT."""
handle_onoff_mode_received(
msg, CONF_AWAY_MODE_STATE_TEMPLATE, '_away')
add_subscription(topics, CONF_AWAY_MODE_STATE_TOPIC,
handle_away_mode_received)
@callback
def handle_aux_mode_received(msg):
"""Handle receiving aux mode via MQTT."""
handle_onoff_mode_received(
msg, CONF_AUX_STATE_TEMPLATE, '_aux')
add_subscription(topics, CONF_AUX_STATE_TOPIC,
handle_aux_mode_received)
@callback
def handle_hold_mode_received(msg):
"""Handle receiving hold mode via MQTT."""
payload = render_template(msg, CONF_HOLD_STATE_TEMPLATE)
if payload == 'off':
payload = None
self._hold = payload
self.async_write_ha_state()
add_subscription(topics, CONF_HOLD_STATE_TOPIC,
handle_hold_mode_received)
self._sub_state = await subscription.async_subscribe_topics(
self.hass, self._sub_state,
topics)
async def async_will_remove_from_hass(self):
"""Unsubscribe when removed."""
self._sub_state = await subscription.async_unsubscribe_topics(
self.hass, self._sub_state)
await MqttAttributes.async_will_remove_from_hass(self)
await MqttAvailability.async_will_remove_from_hass(self)
@property
def should_poll(self):
"""Return the polling state."""
return False
@property
def name(self):
"""Return the name of the climate device."""
return self._config[CONF_NAME]
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return self._unit_of_measurement
@property
def current_temperature(self):
"""Return the current temperature."""
return self._current_temp
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temp
@property
def target_temperature_low(self):
"""Return the low target temperature we try to reach."""
return self._target_temp_low
@property
def target_temperature_high(self):
"""Return the high target temperature we try to reach."""
return self._target_temp_high
@property
def hvac_mode(self):
"""Return current operation ie. heat, cool, idle."""
return self._current_operation
@property
def hvac_modes(self):
"""Return the list of available operation modes."""
return self._config[CONF_MODE_LIST]
@property
def target_temperature_step(self):
"""Return the supported step of target temperature."""
return self._config[CONF_TEMP_STEP]
@property
def preset_mode(self):
"""Return preset mode."""
if self._hold:
return self._hold
if self._away:
return PRESET_AWAY
return None
@property
def preset_modes(self):
"""Return preset modes."""
presets = []
if (self._topic[CONF_AWAY_MODE_STATE_TOPIC] is not None) or \
(self._topic[CONF_AWAY_MODE_COMMAND_TOPIC] is not None):
presets.append(PRESET_AWAY)
presets.extend(self._config[CONF_HOLD_LIST])
return presets
@property
def is_aux_heat(self):
"""Return true if away mode is on."""
return self._aux
@property
def fan_mode(self):
"""Return the fan setting."""
return self._current_fan_mode
@property
def fan_modes(self):
"""Return the list of available fan modes."""
return self._config[CONF_FAN_MODE_LIST]
def _publish(self, topic, payload):
if self._topic[topic] is not None:
mqtt.async_publish(
self.hass, self._topic[topic], payload,
self._config[CONF_QOS], self._config[CONF_RETAIN])
def _set_temperature(self, temp, cmnd_topic, state_topic, attr):
if temp is not None:
if self._topic[state_topic] is None:
# optimistic mode
setattr(self, attr, temp)
if (self._config[CONF_SEND_IF_OFF] or
self._current_operation != HVAC_MODE_OFF):
self._publish(cmnd_topic, temp)
async def async_set_temperature(self, **kwargs):
"""Set new target temperatures."""
if kwargs.get(ATTR_HVAC_MODE) is not None:
operation_mode = kwargs.get(ATTR_HVAC_MODE)
await self.async_set_hvac_mode(operation_mode)
self._set_temperature(
kwargs.get(ATTR_TEMPERATURE), CONF_TEMP_COMMAND_TOPIC,
CONF_TEMP_STATE_TOPIC, '_target_temp')
self._set_temperature(
kwargs.get(ATTR_TARGET_TEMP_LOW), CONF_TEMP_LOW_COMMAND_TOPIC,
CONF_TEMP_LOW_STATE_TOPIC, '_target_temp_low')
self._set_temperature(
kwargs.get(ATTR_TARGET_TEMP_HIGH), CONF_TEMP_HIGH_COMMAND_TOPIC,
CONF_TEMP_HIGH_STATE_TOPIC, '_target_temp_high')
# Always optimistic?
self.async_write_ha_state()
async def async_set_swing_mode(self, swing_mode):
"""Set new swing mode."""
if (self._config[CONF_SEND_IF_OFF] or
self._current_operation != HVAC_MODE_OFF):
self._publish(CONF_SWING_MODE_COMMAND_TOPIC,
swing_mode)
if self._topic[CONF_SWING_MODE_STATE_TOPIC] is None:
self._current_swing_mode = swing_mode
self.async_write_ha_state()
async def async_set_fan_mode(self, fan_mode):
"""Set new target temperature."""
if (self._config[CONF_SEND_IF_OFF] or
self._current_operation != HVAC_MODE_OFF):
self._publish(CONF_FAN_MODE_COMMAND_TOPIC,
fan_mode)
if self._topic[CONF_FAN_MODE_STATE_TOPIC] is None:
self._current_fan_mode = fan_mode
self.async_write_ha_state()
async def async_set_hvac_mode(self, hvac_mode) -> None:
"""Set new operation mode."""
if (self._current_operation == HVAC_MODE_OFF and
hvac_mode != HVAC_MODE_OFF):
self._publish(CONF_POWER_COMMAND_TOPIC,
self._config[CONF_PAYLOAD_ON])
elif (self._current_operation != HVAC_MODE_OFF and
hvac_mode == HVAC_MODE_OFF):
self._publish(CONF_POWER_COMMAND_TOPIC,
self._config[CONF_PAYLOAD_OFF])
self._publish(CONF_MODE_COMMAND_TOPIC,
hvac_mode)
if self._topic[CONF_MODE_STATE_TOPIC] is None:
self._current_operation = hvac_mode
self.async_write_ha_state()
@property
def swing_mode(self):
"""Return the swing setting."""
return self._current_swing_mode
@property
def swing_modes(self):
"""List of available swing modes."""
return self._config[CONF_SWING_MODE_LIST]
async def async_set_preset_mode(self, preset_mode):
"""Set a preset mode."""
if preset_mode == self.preset_mode:
return
# Track if we should optimistic update the state
optimistic_update = False
if self._away:
optimistic_update = optimistic_update or self._set_away_mode(False)
elif preset_mode == PRESET_AWAY:
optimistic_update = optimistic_update or self._set_away_mode(True)
if self._hold:
optimistic_update = optimistic_update or self._set_hold_mode(None)
elif preset_mode not in (None, PRESET_AWAY):
optimistic_update = (optimistic_update or
self._set_hold_mode(preset_mode))
if optimistic_update:
self.async_write_ha_state()
def _set_away_mode(self, state):
"""Set away mode.
Returns if we should optimistically write the state.
"""
self._publish(CONF_AWAY_MODE_COMMAND_TOPIC,
self._config[CONF_PAYLOAD_ON] if state
else self._config[CONF_PAYLOAD_OFF])
if self._topic[CONF_AWAY_MODE_STATE_TOPIC] is not None:
return False
self._away = state
return True
def _set_hold_mode(self, hold_mode):
"""Set hold mode.
Returns if we should optimistically write the state.
"""
self._publish(CONF_HOLD_COMMAND_TOPIC, hold_mode or "off")
if self._topic[CONF_HOLD_STATE_TOPIC] is not None:
return False
self._hold = hold_mode
return True
def _set_aux_heat(self, state):
self._publish(CONF_AUX_COMMAND_TOPIC,
self._config[CONF_PAYLOAD_ON] if state
else self._config[CONF_PAYLOAD_OFF])
if self._topic[CONF_AUX_STATE_TOPIC] is None:
self._aux = state
self.async_write_ha_state()
async def async_turn_aux_heat_on(self):
"""Turn auxiliary heater on."""
self._set_aux_heat(True)
async def async_turn_aux_heat_off(self):
"""Turn auxiliary heater off."""
self._set_aux_heat(False)
@property
def supported_features(self):
"""Return the list of supported features."""
support = 0
if (self._topic[CONF_TEMP_STATE_TOPIC] is not None) or \
(self._topic[CONF_TEMP_COMMAND_TOPIC] is not None):
support |= SUPPORT_TARGET_TEMPERATURE
if (self._topic[CONF_TEMP_LOW_STATE_TOPIC] is not None) or \
(self._topic[CONF_TEMP_LOW_COMMAND_TOPIC] is not None):
support |= SUPPORT_TARGET_TEMPERATURE_RANGE
if (self._topic[CONF_TEMP_HIGH_STATE_TOPIC] is not None) or \
(self._topic[CONF_TEMP_HIGH_COMMAND_TOPIC] is not None):
support |= SUPPORT_TARGET_TEMPERATURE_RANGE
if (self._topic[CONF_FAN_MODE_STATE_TOPIC] is not None) or \
(self._topic[CONF_FAN_MODE_COMMAND_TOPIC] is not None):
support |= SUPPORT_FAN_MODE
if (self._topic[CONF_SWING_MODE_STATE_TOPIC] is not None) or \
(self._topic[CONF_SWING_MODE_COMMAND_TOPIC] is not None):
support |= SUPPORT_SWING_MODE
if (self._topic[CONF_AWAY_MODE_STATE_TOPIC] is not None) or \
(self._topic[CONF_AWAY_MODE_COMMAND_TOPIC] is not None) or \
(self._topic[CONF_HOLD_STATE_TOPIC] is not None) or \
(self._topic[CONF_HOLD_COMMAND_TOPIC] is not None):
support |= SUPPORT_PRESET_MODE
if (self._topic[CONF_AUX_STATE_TOPIC] is not None) or \
(self._topic[CONF_AUX_COMMAND_TOPIC] is not None):
support |= SUPPORT_AUX_HEAT
return support
@property
def min_temp(self):
"""Return the minimum temperature."""
return self._config[CONF_TEMP_MIN]
@property
def max_temp(self):
"""Return the maximum temperature."""
return self._config[CONF_TEMP_MAX]