Skip to content

Commit

Permalink
Add support for turn On/Off Ecodan from HA
Browse files Browse the repository at this point in the history
  • Loading branch information
limkinZero committed Nov 12, 2023
1 parent fa59f02 commit af4bee6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ecodan-ha-local/ehal_hp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,25 @@ namespace ehal::hp
return true;
}

bool set_power_mode(bool on)
{
Message cmd{MsgType::SET_CMD, SetType::BASIC_SETTINGS};
cmd[1] = SET_SETTINGS_FLAG_SYSTEM_MODE_POWER;
cmd[3] = on ? 1 : 0;
{
std::lock_guard<std::mutex>{cmdQueueMutex};
cmdQueue.emplace(std::move(cmd));
}

if (!dispatch_next_cmd())
{
log_web(F("command dispatch failed for DHW force setting!"));
return false;
}

return true;
}

bool set_hp_mode(uint8_t mode)
{
Message cmd{MsgType::SET_CMD, SetType::BASIC_SETTINGS};
Expand Down
1 change: 1 addition & 0 deletions ecodan-ha-local/ehal_hp.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ namespace ehal::hp
bool set_dhw_target_temperature(float value);
bool set_dhw_mode(String mode);
bool set_dhw_force(bool on);
bool set_power_mode(bool on);
bool set_hp_mode(uint8_t mode);

bool begin_connect();
Expand Down
62 changes: 62 additions & 0 deletions ecodan-ha-local/ehal_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ off
}
}

void on_turn_on_off_command(const String& payload)
{
bool turnON = payload == "ON";
if (!hp::set_power_mode(turnON))
{
log_web(F("Failed to set power mode!"));
}
else
{
auto& status = hp::get_status();
std::lock_guard<hp::Status> lock{status};
publish_sensor_status<String>(F("mode_power"), status.power_as_string());
}
}

void mqtt_callback(String& topic, String& payload)
{
try
Expand All @@ -252,6 +267,7 @@ off
String climateEntity = unique_entity_name(F("climate_control"));
String tempCmdTopic = config.MqttTopic + "/" + climateEntity + F("/temp_cmd");
String dhwForceCmdTopic = config.MqttTopic + "/" + unique_entity_name(F("force_dhw")) + F("/set");
String turnOnOffCmdTopic = config.MqttTopic + "/" + unique_entity_name(F("turn_on_off_hp")) + F("/set");
String dhwTempCmdTopic = config.MqttTopic + "/" + unique_entity_name(F("dhw_water_heater")) + F("/set");
String dhwModeCmdTopic = config.MqttTopic + "/" + unique_entity_name(F("dhw_mode")) + F("/set");
String shModeCmdTopic = config.MqttTopic + "/" + unique_entity_name(F("sh_mode")) + F("/set");
Expand All @@ -277,6 +293,10 @@ off
{
on_force_dhw_command(payload);
}
else if (turnOnOffCmdTopic == topic)
{
on_turn_on_off_command(payload);
}
}
catch (std::exception const& ex)
{
Expand Down Expand Up @@ -463,6 +483,39 @@ off
return true;
}

bool publish_ha_turn_on_off_auto_discover()
{
// https://www.home-assistant.io/integrations/switch.mqtt/
String uniqueName = unique_entity_name(F("turn_on_off_hp"));

const auto& config = config_instance();
String discoveryTopic = String(F("homeassistant/switch/")) + uniqueName + F("/config");
String stateTopic = config.MqttTopic + "/" + unique_entity_name(F("mode_power")) + F("/state");
String cmdTopic = config.MqttTopic + "/" + uniqueName + F("/set");

DynamicJsonDocument payloadJson(8192);
payloadJson[F("name")] = uniqueName;
payloadJson[F("unique_id")] = uniqueName;
payloadJson[F("icon")] = F("mdi:power");

add_discovery_device_object(payloadJson);

payloadJson[F("stat_t")] = stateTopic;
payloadJson[F("stat_t_tpl")] = F("{{ value }}");
payloadJson[F("stat_on")] = F("on");
payloadJson[F("stat_off")] = F("off");
payloadJson[F("cmd_t")] = cmdTopic;
payloadJson[F("cmd_tpl")] = F("{{ value }}");

if (!publish_mqtt(discoveryTopic, payloadJson, /* retain =*/true))
{
log_web(F("Failed to publish homeassistant turn On/Off HP entity auto-discover"));
return false;
}

return true;
}

bool publish_ha_set_dhw_temp_auto_discover()
{
// https://www.home-assistant.io/integrations/water_heater.mqtt/
Expand Down Expand Up @@ -679,6 +732,9 @@ off
if (!publish_ha_force_dhw_auto_discover())
anyFailed = true;

if (!publish_ha_turn_on_off_auto_discover())
anyFailed = true;

if (!publish_ha_set_dhw_temp_auto_discover())
anyFailed = true;

Expand Down Expand Up @@ -888,6 +944,12 @@ off
return false;
}

if (!mqttClient.subscribe(config.MqttTopic + "/" + unique_entity_name(F("turn_on_off_hp")) + F("/set")))
{
log_web(F("Failed to subscribe to turn ON/OFF command topic!"));
return false;
}

if (!mqttClient.subscribe(config.MqttTopic + "/" + unique_entity_name(F("dhw_water_heater")) + F("/set")))
{
log_web(F("Failed to subscribe to DHW temperature topic!"));
Expand Down
1 change: 1 addition & 0 deletions ecodan-ha-local/ehal_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace ehal::hp
#define SET_SETTINGS_FLAG_HP_MODE 0x08
#define SET_SETTINGS_FLAG_DHW_MODE 0x04
#define SET_SETTINGS_FLAG_MODE_TOGGLE 0x1
#define SET_SETTINGS_FLAG_SYSTEM_MODE_POWER 0x01

enum class SetZone
{
Expand Down

0 comments on commit af4bee6

Please sign in to comment.