From 95bed62bd12fa94f147f4931db9c81070653eb55 Mon Sep 17 00:00:00 2001 From: halfbakery Date: Mon, 29 Jun 2020 19:38:50 +0200 Subject: [PATCH 1/2] Do not announce shutter controlling relays to Home Assistant --- tasmota/xdrv_12_home_assistant.ino | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index df53084a8..5163710cf 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -208,6 +208,7 @@ void HAssAnnounceRelayLight(void) uint8_t TuyaRel = 0; uint8_t TuyaRelInv = 0; uint8_t TuyaDim = 0; + uint8_t shutter_mask = 0; #ifdef ESP8266 if (PWM_DIMMER == my_module_type ) { PwmMod = true; } // @@ -225,6 +226,17 @@ void HAssAnnounceRelayLight(void) if (!PwmMulti) { max_lights = 2;} } +#ifdef USE_SHUTTER + if (Settings.flag3.shutter_mode) { + for (uint32_t i = 0; i < MAX_SHUTTERS; i++) { + if (Settings.shutter_startrelay[i] > 0 && Settings.shutter_startrelay[i] <= MAX_RELAYS) { + bitSet(shutter_mask, Settings.shutter_startrelay[i] -1); + bitSet(shutter_mask, Settings.shutter_startrelay[i]); + } + } + } +#endif + for (uint32_t i = 1; i <= MAX_RELAYS; i++) { @@ -248,7 +260,9 @@ void HAssAnnounceRelayLight(void) snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/%s/%s/config"), (is_topic_light) ? "light" : "switch", unique_id); - if ((i < Light.device) && !RelayX) { + if (bitRead(shutter_mask, i-1)) { + // suppress shutter relays + } else if ((i < Light.device) && !RelayX) { err_flag = true; AddLog_P2(LOG_LEVEL_ERROR, PSTR("%s"), kHAssError2); } else { From 50a9258eac90d32b2ffaca089c4c48982bce6d32 Mon Sep 17 00:00:00 2001 From: halfbakery Date: Mon, 29 Jun 2020 19:44:26 +0200 Subject: [PATCH 2/2] Home Assistant shutter autodiscovery --- tasmota/xdrv_12_home_assistant.ino | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index 5163710cf..005daadfc 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -108,6 +108,21 @@ const char HASS_DISCOVER_LIGHT_SCHEME[] PROGMEM = "\"fx_val_tpl\":\"{{value_json." D_CMND_SCHEME "}}\"," "\"fx_list\":[\"0\",\"1\",\"2\",\"3\",\"4\"]"; // string list with reference to scheme parameter. +const char HASS_DISCOVER_SHUTTER_BASE[] PROGMEM = + ",\"cmd_t\":\"%s\"," // cmnd/%topic%/Backlog + "\"pl_open\":\"ShutterOpen%d\"," // 1 + "\"pl_cls\":\"ShutterClose%d\"," // 1 + "\"pl_stop\":\"ShutterStop%d\"," // 1 + "\"opt\":false," + "\"ret\":false," + "\"qos\":1"; + +const char HASS_DISCOVER_SHUTTER_POS[] PROGMEM = + ",\"pos_t\":\"%s%d\"," // stat/%topic%/SHUTTER1 + "\"pos_clsd\":0," + "\"pos_open\":100," + "\"set_pos_t\":\"%s%d\""; // cmnd/%topic%/ShutterPosition1 + const char HASS_DISCOVER_SENSOR_HASS_STATUS[] PROGMEM = ",\"json_attr_t\":\"%s\"," "\"unit_of_meas\":\"%%\"," @@ -726,6 +741,48 @@ void HAssAnnounceSensors(void) } while (hass_xsns_index != 0); } +void HAssAnnounceShutters(void) +{ +#ifdef USE_SHUTTER + char stopic[TOPSZ]; + char stemp1[TOPSZ]; + char stemp2[TOPSZ]; + char unique_id[30]; + + for (uint32_t i = 0; i < MAX_SHUTTERS; i++) { + mqtt_data[0] = '\0'; // Clear retained message + + snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_SHT_%d"), ESP_getChipId(), i + 1); + snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/cover/%s/config"), unique_id); + + if (Settings.flag.hass_discovery && Settings.flag3.shutter_mode && Settings.shutter_startrelay[i] > 0 && Settings.shutter_startrelay[i] <= MAX_RELAYS) { + if (i > MAX_FRIENDLYNAMES) { + snprintf_P(stemp1, sizeof(stemp1), PSTR("%s Shutter %d"), SettingsText(SET_DEVICENAME), i + 1); + } else { + snprintf_P(stemp1, sizeof(stemp1), PSTR("%s"), SettingsText(SET_FRIENDLYNAME1 + i)); + } + GetTopic_P(stemp2, TELE, mqtt_topic, D_RSLT_STATE); + Response_P(HASS_DISCOVER_BASE, stemp1, stemp2); + + GetTopic_P(stemp1, TELE, mqtt_topic, S_LWT); + TryResponseAppend_P(HASS_DISCOVER_SENSOR_LWT, stemp1); + + GetTopic_P(stemp1, CMND, mqtt_topic, PSTR("Backlog")); + TryResponseAppend_P(HASS_DISCOVER_SHUTTER_BASE, stemp1, i + 1, i + 1, i + 1); + + GetTopic_P(stemp1, STAT, mqtt_topic, PSTR("SHUTTER")); + GetTopic_P(stemp2, CMND, mqtt_topic, PSTR("ShutterPosition")); + TryResponseAppend_P(HASS_DISCOVER_SHUTTER_POS, stemp1, i + 1, stemp2, i + 1); + + TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP_getChipId()); + TryResponseAppend_P(PSTR("}")); + } + + MqttPublish(stopic, true); + } +#endif +} + void HAssAnnounceDeviceInfoAndStatusSensor(void) { char stopic[TOPSZ]; @@ -801,6 +858,9 @@ void HAssDiscovery(void) // Send info about relays and lights HAssAnnounceRelayLight(); + // Send info about shutters + HAssAnnounceShutters(); + // Send info about status sensor HAssAnnounceDeviceInfoAndStatusSensor(); }