Skip to content

Commit

Permalink
Holiday Mode (#1)
Browse files Browse the repository at this point in the history
* Add Home Assistant holiday mode sensor
* Add Home Assistant holiday mode switch
* Update .gitignore
  • Loading branch information
drnichols committed Apr 29, 2024
1 parent c301bb5 commit 5e97c32
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 21 deletions.
20 changes: 20 additions & 0 deletions ecodan-ha-local/ehal_hp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,26 @@ namespace ehal::hp
return true;
}

bool set_holiday_mode(bool on)
{
Message cmd{MsgType::SET_CMD, SetType::DHW_SETTING};
cmd[1] = SET_SETTINGS_HOLIDAY_MODE_TOGGLE;
cmd[4] = on ? 1 : 0;

{
std::lock_guard<std::mutex> lock{cmdQueueMutex};
cmdQueue.emplace(std::move(cmd));
}

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

return true;
}

bool set_power_mode(bool on)
{
Message cmd{MsgType::SET_CMD, SetType::BASIC_SETTINGS};
Expand Down
21 changes: 21 additions & 0 deletions ecodan-ha-local/ehal_hp.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ namespace ehal::hp
ON = 1
};

enum class HolMode : uint8_t
{
OFF = 0,
ON = 1
};

enum class OperationMode : uint8_t
{
OFF = 0,
Expand Down Expand Up @@ -68,6 +74,7 @@ namespace ehal::hp
// Modes
PowerMode Power;
OperationMode Operation;
HolMode setHolidayMode;
bool HolidayMode;
bool DhwTimerMode;
DhwMode HotWaterMode;
Expand Down Expand Up @@ -140,6 +147,19 @@ namespace ehal::hp
}
}

String holmode_as_string()
{
switch (setHolidayMode)
{
case HolMode::ON:
return F("on");
case HolMode::OFF:
return F("off");
default:
return F("unknown");
}
}

String operation_as_string()
{
switch (Operation)
Expand Down Expand Up @@ -254,6 +274,7 @@ namespace ehal::hp
bool set_dhw_force(bool on);
bool set_power_mode(bool on);
bool set_hp_mode(uint8_t mode);
bool set_holiday_mode(bool on);

bool begin_connect();
bool begin_update_status();
Expand Down
76 changes: 75 additions & 1 deletion ecodan-ha-local/ehal_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@ off
}
}

void holiday_mode_on_off_command(const String& payload)
{
bool turnON = payload == "ON";

if (!hp::set_holiday_mode(turnON))
{
log_web(F("Failed to set holiday mode!"));
}
else
{
auto& status = hp::get_status();
std::lock_guard<hp::Status> lock{status};
if (turnON)
{
status.setHolidayMode = hp::Status::HolMode::ON;
}
else
{
status.setHolidayMode = hp::Status::HolMode::OFF;
}
publish_binary_sensor_status(F("holiday_mode"), status.holmode_as_string());
}
}

void mqtt_callback(String& topic, String& payload)
{
try
Expand All @@ -310,7 +334,8 @@ off
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");

String setHolidayModeTopic = config.MqttTopic + "/" + unique_entity_name(F("holiday_mode")) + F("/set");

log_web(F("MQTT topic received: %s: '%s'"), topic.c_str(), payload.c_str());

if (tempCmdTopic == topic)
Expand Down Expand Up @@ -341,6 +366,10 @@ off
{
on_turn_on_off_command(payload);
}
else if (setHolidayModeTopic == topic)
{
holiday_mode_on_off_command(payload);
}
}
catch (std::exception const& ex)
{
Expand Down Expand Up @@ -551,6 +580,38 @@ off
return true;
}

bool publish_ha_switch_holiday_mode_auto_discover()
{
// https://www.home-assistant.io/integrations/switch.mqtt/
String uniqueName = unique_entity_name(F("holiday_mode"));
const auto& config = config_instance();
String discoveryTopic = String(F("homeassistant/switch/")) + uniqueName + F("/config");
String stateTopic = config.MqttTopic + "/" + uniqueName + F("/state");
String cmdTopic = config.MqttTopic + "/" + uniqueName + F("/set");

JsonDocument doc;
JsonObject payloadJson = doc.to<JsonObject>();
payloadJson[F("name")] = uniqueName;
payloadJson[F("unique_id")] = uniqueName;
payloadJson[F("icon")] = F("mdi:toggle-switch-variant");

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, doc, /* retain =*/true))
{
log_web(F("Failed to publish homeassistant Holiday Mode entity auto-discover"));
return false;
}
return true;
}

bool publish_ha_set_z1_flow_target_auto_discover()
{
// https://www.home-assistant.io/integrations/number.mqtt/
Expand Down Expand Up @@ -912,6 +973,9 @@ off
if (!publish_ha_set_sh_mode_auto_discover())
return;

if (!publish_ha_switch_holiday_mode_auto_discover())
return;

if (!publish_ha_binary_sensor_auto_discover(F("mode_defrost")))
return;

Expand Down Expand Up @@ -1014,6 +1078,8 @@ off
if (!publish_ha_float_sensor_auto_discover(F("cool_cop"), SensorType::COP))
return;

if (!publish_ha_binary_sensor_auto_discover(F("holiday_mode")))
return;
// Diagnostic sensors
if (!publish_ha_diagnostic_sensor_auto_discover(F("Heat pump connection state"), SensorType::CONNECTIVITY))
return;
Expand Down Expand Up @@ -1198,6 +1264,8 @@ off
if (!publish_sensor_status<float>(F("cool_cop"), status.EnergyConsumedCooling > 0.0f ? status.EnergyDeliveredCooling / status.EnergyConsumedCooling : 0.0f))
return;

if (!publish_binary_sensor_status(F("holiday_mode"), status.HolidayMode))
return;
// Diagnostic
if (!publish_binary_sensor_status(F("Heat pump connection state"), hp::is_connected()))
return;
Expand Down Expand Up @@ -1287,6 +1355,12 @@ off
return false;
}

if (!mqttClient.subscribe(config.MqttTopic + "/" + unique_entity_name(F("holiday_mode")) + F("/set")))
{
log_web(F("Failed to subscribe to Holiday Mode command topic!"));
return false;
}

log_web(F("Successfully established MQTT client connection!"));
}

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_HOLIDAY_MODE_TOGGLE 0x2

enum class SetZone
{
Expand Down
20 changes: 0 additions & 20 deletions platformio.ini

This file was deleted.

0 comments on commit 5e97c32

Please sign in to comment.