New climate / thermostat implementation tips

I’ve used this as reference: https://github.com/daveNewcastle/HassIO-config/commit/99d6d704bfede904cddf6bef1e1d26fa68f8ad6a#diff-b93b5f54ec74c57c171d6b2efb485dab and got it working! I’ve created a ~/.homeassistant/custom_components/climate/InComfort.py file with:

import json
import logging

import voluptuous as vol
import homeassistant.helpers.config_validation as cv

from urllib.request import urlopen
from homeassistant.components.climate import (
    STATE_HEAT, ClimateDevice, SUPPORT_TARGET_TEMPERATURE, PLATFORM_SCHEMA)
from homeassistant.const import (
    TEMP_CELSIUS, ATTR_TEMPERATURE)

CONF_HOST = 'host'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_HOST): cv.string,
})

def _lsbmsb(lsb, msb):
    return (lsb + msb*256) / 100.0

def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the InComfort thermostat."""

    host = config.get(CONF_HOST)

    add_devices([InComfortThermostat(host)])

class InComfortThermostat(ClimateDevice):
    """Representation of a InComfort Thermostat device."""

    def __init__(self, host):
        """Initialize the thermostat."""
        self.host = host
        self._name = "InComfort"
        self._current_temperature = None
        self._target_temperature = None
        self.data = None

    @property
    def supported_features(self):
        """Return the list of supported features."""
        return SUPPORT_TARGET_TEMPERATURE

    @property
    def name(self):
        """Return the name of the thermostat, if any."""
        return self._name

    @property
    def temperature_unit(self):
        """Return the unit of measurement which this thermostat uses."""
        return TEMP_CELSIUS

    @property
    def current_temperature(self):
        """Return the current temperature."""
        if self.data is not None:
            self._current_temperature = _lsbmsb(self.data['room_temp_1_lsb'], self.data['room_temp_1_msb'])
        else:
            self._current_temperature = None
        return self._current_temperature

    @property
    def target_temperature(self):
        """Return the temperature we try to reach."""
        if self.data is not None:
            self._target_temperature = _lsbmsb(self.data['room_temp_set_1_lsb'], self.data['room_temp_set_1_msb'])
        else:
            self._target_temperature = None
        return self._target_temperature

    @property
    def should_poll(self):
        """Return the polling state."""
        return True

    def set_temperature(self, **kwargs):
        """Set new target temperature."""
        temperature = kwargs.get(ATTR_TEMPERATURE)
        if temperature is None:
            return

        urlopen('http://' + self.host + '/data.json?heater=0&thermostat=0&setpoint=' + str((min(max(temperature, 5), 30) - 5.0) * 10))
        self._target_temperature = temperature

    def update(self):
        """Get the latest data."""
        response = urlopen('http://' + self.host + '/data.json?heater=0')
        string = response.read().decode('utf-8')
        self.data = json.loads(string)

And added this in my ~/.homeassistant/configuration.yaml file:

climate:
  # InComfort
  - platform: InComfort
    host: 192.168.1.139

But…

  1. I did not manage to get it working with the library (https://github.com/bwesterb/incomfort). Is it just a CLI tool?
  2. The current setpoint is called “Unknown”, see:

    How can I change that?
  3. When Home Assistant starts it just says “Unknown” until 1 minute passes and the update is triggered, see:

    How can I make sure the update is triggered on load?
  4. When I change the temperature it’s not directly reflected while I’m setting it after changing with self._target_temperature = temperature

Any tips? Or other suggestions to improve this?