Hi there,
this is working for me:
configuration.yaml:
climate:
# TODO: may make sense to use additional mqtt sensors as state topics
- platform: mqtt
name: "WZ Heizung"
command_topic: "/living-room/fhem-fht80b-desired-temp/set"
measured_state_topic: "/living-room/fhem-fht80b-measured-temp"
desired_state_topic: "/living-room/fhem-fht80b-desired-temp"
And this is my ~/.home-assistant/custom_components/climate/mqtt.py:
"""
Support for MQTT heaters.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.mqtt/
"""
import logging
import voluptuous as vol
from homeassistant.components.mqtt import (
CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN)
from homeassistant.components.climate import (
ClimateDevice, )
from homeassistant.const import (
CONF_NAME, CONF_VALUE_TEMPLATE, TEMP_CELSIUS, ATTR_TEMPERATURE)
import homeassistant.components.mqtt as mqtt
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['mqtt']
DEFAULT_NAME = 'MQTT Climate'
PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the MQTT climate device."""
value_template = config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
value_template.hass = hass
add_devices([MqttClimate(
hass,
config.get(CONF_NAME),
config.get('desired_state_topic'),
config.get('measured_state_topic'),
config.get(CONF_COMMAND_TOPIC),
config.get(CONF_QOS),
config.get(CONF_RETAIN),
value_template,
)])
class MqttClimate(ClimateDevice):
"""Representation of a heater that can be toggled using MQTT."""
def __init__(self, hass, name, desired_state_topic, measured_state_topic, command_topic, qos, retain,
value_template):
"""Initialize the MQTT switch."""
self._state = False
self._hass = hass
self._name = name
self._desired_state_topic = desired_state_topic
self._measured_state_topic = measured_state_topic
self._command_topic = command_topic
self._qos = qos
self._retain = retain
self._current_temperature = None
self._target_temperature = None
def message_received(topic, payload, qos):
"""A new MQTT message has been received."""
if value_template is not None:
payload = value_template.render_with_possible_json_value(
payload)
if topic == self._desired_state_topic:
self._target_temperature = payload
elif topic == self._measured_state_topic:
self._current_temperature = payload
else:
print("unknown topic")
self.update_ha_state()
for topic in [self._desired_state_topic, self._measured_state_topic]:
mqtt.subscribe(
hass, topic, message_received, self._qos)
@property
def should_poll(self):
"""Polling not needed for a demo climate device."""
return False
@property
def name(self):
"""Return the name of the climate device."""
return self._name
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
# we need the target_temperature property not to
# be None or we won't be able to use the slider widget in the web interface
# This is not very convenient because it may be None initially, or unknown, especially
# in "optimistic" settings.
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
@property
def current_temperature(self):
"""Return the temperature we try to reach."""
return self._current_temperature
def set_temperature(self, **kwargs):
"""Set new target temperatures."""
if kwargs.get(ATTR_TEMPERATURE) is not None:
# This is also be set via the mqtt callback
self._target_temperature = kwargs.get(ATTR_TEMPERATURE)
self._publish_temperature()
self.update_ha_state()
def _publish_temperature(self):
if self._target_temperature is None:
return
mqtt.publish(self.hass, self._command_topic, self._target_temperature,
self._qos, self._retain)