Skip to content

Instantly share code, notes, and snippets.

@ajvpot
Last active March 8, 2020 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajvpot/d6f86bcd1d8ff18108a9021c340d56b7 to your computer and use it in GitHub Desktop.
Save ajvpot/d6f86bcd1d8ff18108a9021c340d56b7 to your computer and use it in GitHub Desktop.
SwiCago/HeatPump ESPHome Component
#include "esphome.h"
#include "HeatPump.h"
class MitsubishiClimate : public PollingComponent, public Climate
{
public:
HeatPump hp;
MitsubishiClimate() : PollingComponent(10000)
{
this->hp.setStatusChangedCallback(std::bind(&MitsubishiClimate::syncState, this));
this->hp.setSettingsChangedCallback(std::bind(&MitsubishiClimate::syncState, this));
}
void setup() override
{
this->update();
}
void loop() override
{
if (hp.isConnected())
hp.sync();
}
void syncState()
{
this->mode = CLIMATE_MODE_OFF;
const auto mode = hp.getModeSetting();
if (hp.getPowerSettingBool() && mode != NULL)
{
if (strcmp(mode, "COOL") == 0)
{
this->mode = CLIMATE_MODE_COOL;
}
else if (strcmp(mode, "HEAT") == 0)
{
this->mode = CLIMATE_MODE_HEAT;
}
else if (strcmp(mode, "AUTO") == 0)
{
this->mode = CLIMATE_MODE_AUTO;
}
}
this->target_temperature = hp.getTemperature();
this->current_temperature = hp.getRoomTemperature();
this->publish_state();
}
// called every 10s
void update() override
{
if (!hp.isConnected())
{
// try to connect once
hp.connect(&Serial, false);
}
}
void control(const ClimateCall &call) override
{
if (call.get_mode().has_value())
{
// User requested mode change
ClimateMode mode = *call.get_mode();
switch (mode)
{
case (1):
hp.setPowerSetting(true);
hp.setModeSetting("AUTO");
break;
case (2):
hp.setPowerSetting(true);
hp.setModeSetting("COOL");
break;
case (3):
hp.setPowerSetting(true);
hp.setModeSetting("HEAT");
break;
default:
hp.setPowerSetting(false);
break;
}
}
if (call.get_target_temperature().has_value())
{
// User requested target temperature change
float temp = *call.get_target_temperature();
hp.setTemperature(temp);
}
hp.update();
this->update();
}
ClimateTraits traits() override
{
auto ct = new ClimateTraits;
ct->set_supports_auto_mode(true);
ct->set_supports_heat_mode(true);
ct->set_supports_cool_mode(true);
ct->set_supports_current_temperature(true);
ct->set_visual_temperature_step(1.0);
return *ct;
}
};
esphome:
name: heatpump_livingroom
platform: ESP8266
board: esp01_1m
includes:
- climate_mitsubishi.h
libraries:
"HeatPump"
climate:
- platform: custom
lambda: |-
auto my_custom_climate = new MitsubishiClimate();
App.register_component(my_custom_climate);
return {my_custom_climate};
climates:
- name: "Living Room AC"
logger:
baud_rate: 0
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment