|
| 1 | +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <sdkconfig.h> |
| 16 | +#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL |
| 17 | + |
| 18 | +#include <Matter.h> |
| 19 | +#include <MatterEndpoints/MatterThermostat.h> |
| 20 | + |
| 21 | +using namespace esp_matter; |
| 22 | +using namespace esp_matter::endpoint; |
| 23 | +using namespace chip::app::Clusters; |
| 24 | + |
| 25 | +// string helper for the THERMOSTAT MODE |
| 26 | +const char *MatterThermostat::thermostatModeString[5] = {"OFF", "AUTO", "UNKNOWN", "COOL", "HEAT"}; |
| 27 | + |
| 28 | +// endpoint for color light device |
| 29 | +namespace esp_matter { |
| 30 | +using namespace cluster; |
| 31 | +namespace endpoint { |
| 32 | +namespace multi_mode_thermostat { |
| 33 | +typedef struct config { |
| 34 | + cluster::descriptor::config_t descriptor; |
| 35 | + cluster::identify::config_t identify; |
| 36 | + cluster::scenes_management::config_t scenes_management; |
| 37 | + cluster::groups::config_t groups; |
| 38 | + cluster::thermostat::config_t thermostat; |
| 39 | +} config_t; |
| 40 | + |
| 41 | +uint32_t get_device_type_id() { |
| 42 | + return ESP_MATTER_THERMOSTAT_DEVICE_TYPE_ID; |
| 43 | +} |
| 44 | + |
| 45 | +uint8_t get_device_type_version() { |
| 46 | + return ESP_MATTER_THERMOSTAT_DEVICE_TYPE_VERSION; |
| 47 | +} |
| 48 | + |
| 49 | +esp_err_t add(endpoint_t *endpoint, config_t *config) { |
| 50 | + if (!endpoint) { |
| 51 | + log_e("Endpoint cannot be NULL"); |
| 52 | + return ESP_ERR_INVALID_ARG; |
| 53 | + } |
| 54 | + esp_err_t err = add_device_type(endpoint, get_device_type_id(), get_device_type_version()); |
| 55 | + if (err != ESP_OK) { |
| 56 | + log_e("Failed to add device type id:%" PRIu32 ",err: %d", get_device_type_id(), err); |
| 57 | + return err; |
| 58 | + } |
| 59 | + |
| 60 | + descriptor::create(endpoint, &(config->descriptor), CLUSTER_FLAG_SERVER); |
| 61 | + identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER); |
| 62 | + groups::create(endpoint, &(config->groups), CLUSTER_FLAG_SERVER); |
| 63 | + uint32_t thermostatFeatures = 0; |
| 64 | + switch (config->thermostat.control_sequence_of_operation) { |
| 65 | + case MatterThermostat::THERMOSTAT_SEQ_OP_COOLING: |
| 66 | + case MatterThermostat::THERMOSTAT_SEQ_OP_COOLING_REHEAT: thermostatFeatures = cluster::thermostat::feature::cooling::get_id(); break; |
| 67 | + case MatterThermostat::THERMOSTAT_SEQ_OP_HEATING: |
| 68 | + case MatterThermostat::THERMOSTAT_SEQ_OP_HEATING_REHEAT: thermostatFeatures = cluster::thermostat::feature::heating::get_id(); break; |
| 69 | + case MatterThermostat::THERMOSTAT_SEQ_OP_COOLING_HEATING: |
| 70 | + case MatterThermostat::THERMOSTAT_SEQ_OP_COOLING_HEATING_REHEAT: |
| 71 | + thermostatFeatures = cluster::thermostat::feature::cooling::get_id() | cluster::thermostat::feature::heating::get_id(); |
| 72 | + break; |
| 73 | + } |
| 74 | + cluster::thermostat::create(endpoint, &(config->thermostat), CLUSTER_FLAG_SERVER, thermostatFeatures); |
| 75 | + return ESP_OK; |
| 76 | +} |
| 77 | + |
| 78 | +endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data) { |
| 79 | + endpoint_t *endpoint = endpoint::create(node, flags, priv_data); |
| 80 | + add(endpoint, config); |
| 81 | + return endpoint; |
| 82 | +} |
| 83 | +} // namespace multi_mode_thermostat |
| 84 | +} // namespace endpoint |
| 85 | +} // namespace esp_matter |
| 86 | + |
| 87 | +bool MatterThermostat::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) { |
| 88 | + bool ret = true; |
| 89 | + if (!started) { |
| 90 | + log_e("Matter Thermostat device has not begun."); |
| 91 | + return false; |
| 92 | + } |
| 93 | + log_d("Thermostat Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32); |
| 94 | + |
| 95 | + if (cluster_id == Thermostat::Id) { |
| 96 | + switch (attribute_id) { |
| 97 | + case Thermostat::Attributes::SystemMode::Id: |
| 98 | + if (_onChangeModeCB != NULL) { |
| 99 | + ret &= _onChangeModeCB((ThermostatMode_t)val->val.u8); |
| 100 | + } |
| 101 | + if (_onChangeCB != NULL) { |
| 102 | + ret &= _onChangeCB(); |
| 103 | + } |
| 104 | + if (ret == true) { |
| 105 | + currentMode = (ThermostatMode_t)val->val.u8; |
| 106 | + log_v("Thermostat Mode updated to %d", val->val.u8); |
| 107 | + } |
| 108 | + break; |
| 109 | + case Thermostat::Attributes::LocalTemperature::Id: |
| 110 | + if (_onChangeTemperatureCB != NULL) { |
| 111 | + ret &= _onChangeTemperatureCB((float)val->val.i16 / 100.00); |
| 112 | + } |
| 113 | + if (_onChangeCB != NULL) { |
| 114 | + ret &= _onChangeCB(); |
| 115 | + } |
| 116 | + if (ret == true) { |
| 117 | + localTemperature = val->val.i16; |
| 118 | + log_v("Local Temperature updated to %.01fC", (float)val->val.i16 / 100.00); |
| 119 | + } |
| 120 | + break; |
| 121 | + case Thermostat::Attributes::OccupiedCoolingSetpoint::Id: |
| 122 | + if (_onChangeCoolingSetpointCB != NULL) { |
| 123 | + ret &= _onChangeCoolingSetpointCB((float)val->val.i16 / 100.00); |
| 124 | + } |
| 125 | + if (_onChangeCB != NULL) { |
| 126 | + ret &= _onChangeCB(); |
| 127 | + } |
| 128 | + if (ret == true) { |
| 129 | + coolingSetpointTemperature = val->val.i16; |
| 130 | + log_v("Cooling Setpoint updated to %.01fC", (float)val->val.i16 / 100.00); |
| 131 | + } |
| 132 | + break; |
| 133 | + case Thermostat::Attributes::OccupiedHeatingSetpoint::Id: |
| 134 | + if (_onChangeHeatingSetpointCB != NULL) { |
| 135 | + ret &= _onChangeHeatingSetpointCB((float)val->val.i16 / 100.00); |
| 136 | + } |
| 137 | + if (_onChangeCB != NULL) { |
| 138 | + ret &= _onChangeCB(); |
| 139 | + } |
| 140 | + if (ret == true) { |
| 141 | + heatingSetpointTemperature = val->val.i16; |
| 142 | + log_v("Heating Setpoint updated to %.01fC", (float)val->val.i16 / 100.00); |
| 143 | + } |
| 144 | + break; |
| 145 | + default: log_w("Unhandled Thermostat Attribute ID: %u", attribute_id); break; |
| 146 | + } |
| 147 | + } |
| 148 | + return ret; |
| 149 | +} |
| 150 | + |
| 151 | +MatterThermostat::MatterThermostat() {} |
| 152 | + |
| 153 | +MatterThermostat::~MatterThermostat() { |
| 154 | + end(); |
| 155 | +} |
| 156 | + |
| 157 | +bool MatterThermostat::begin(ControlSequenceOfOperation_t _controlSequence, ThermostatAutoMode_t _autoMode) { |
| 158 | + ArduinoMatter::_init(); |
| 159 | + |
| 160 | + if (getEndPointId() != 0) { |
| 161 | + log_e("Matter Thermostat with Endpoint Id %d device has already been created.", getEndPointId()); |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + // check if auto mode is allowed with the control sequence of operation - only allowed for Cooling & Heating |
| 166 | + if (_autoMode == THERMOSTAT_AUTO_MODE_ENABLED && _controlSequence != THERMOSTAT_SEQ_OP_COOLING_HEATING |
| 167 | + && _controlSequence != THERMOSTAT_SEQ_OP_COOLING_HEATING_REHEAT) { |
| 168 | + log_e("Thermostat in Auto Mode requires a Cooling & Heating Control Sequence of Operation."); |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + const int16_t _localTemperature = 2000; // initial value to be automatically changed by the Matter Thermostat |
| 173 | + const int16_t _coolingSetpointTemperature = 2400; // 24C cooling setpoint |
| 174 | + const int16_t _heatingSetpointTemperature = 1600; // 16C heating setpoint |
| 175 | + const ThermostatMode_t _currentMode = THERMOSTAT_MODE_OFF; |
| 176 | + |
| 177 | + multi_mode_thermostat::config_t thermostat_config; |
| 178 | + thermostat_config.thermostat.control_sequence_of_operation = (uint8_t)_controlSequence; |
| 179 | + thermostat_config.thermostat.cooling.occupied_cooling_setpoint = _coolingSetpointTemperature; |
| 180 | + thermostat_config.thermostat.heating.occupied_heating_setpoint = _heatingSetpointTemperature; |
| 181 | + thermostat_config.thermostat.system_mode = (uint8_t)_currentMode; |
| 182 | + thermostat_config.thermostat.local_temperature = _localTemperature; |
| 183 | + |
| 184 | + // endpoint handles can be used to add/modify clusters |
| 185 | + endpoint_t *endpoint = multi_mode_thermostat::create(node::get(), &thermostat_config, ENDPOINT_FLAG_NONE, (void *)this); |
| 186 | + if (endpoint == nullptr) { |
| 187 | + log_e("Failed to create Thermostat endpoint"); |
| 188 | + return false; |
| 189 | + } |
| 190 | + if (_autoMode == THERMOSTAT_AUTO_MODE_ENABLED) { |
| 191 | + cluster_t *cluster = cluster::get(endpoint, Thermostat::Id); |
| 192 | + thermostat_config.thermostat.auto_mode.min_setpoint_dead_band = kDefaultDeadBand; // fixed by default to 2.5C |
| 193 | + cluster::thermostat::feature::auto_mode::add(cluster, &thermostat_config.thermostat.auto_mode); |
| 194 | + } |
| 195 | + |
| 196 | + controlSequence = _controlSequence; |
| 197 | + autoMode = _autoMode; |
| 198 | + coolingSetpointTemperature = _coolingSetpointTemperature; |
| 199 | + heatingSetpointTemperature = _heatingSetpointTemperature; |
| 200 | + localTemperature = _localTemperature; |
| 201 | + currentMode = _currentMode; |
| 202 | + |
| 203 | + setEndPointId(endpoint::get_id(endpoint)); |
| 204 | + log_i("Thermostat created with endpoint_id %d", getEndPointId()); |
| 205 | + started = true; |
| 206 | + return true; |
| 207 | +} |
| 208 | + |
| 209 | +void MatterThermostat::end() { |
| 210 | + started = false; |
| 211 | +} |
| 212 | + |
| 213 | +bool MatterThermostat::setMode(ThermostatMode_t _mode) { |
| 214 | + if (!started) { |
| 215 | + log_e("Matter Thermostat device has not begun."); |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + if (autoMode == THERMOSTAT_AUTO_MODE_DISABLED && _mode == THERMOSTAT_MODE_AUTO) { |
| 220 | + log_e("Thermostat can't set Auto Mode."); |
| 221 | + return false; |
| 222 | + } |
| 223 | + // check if the requested mode is valid based on the control sequence of operation |
| 224 | + // no restrictions for OFF mode |
| 225 | + if (_mode != THERMOSTAT_MODE_OFF) { |
| 226 | + // check HEAT, COOL and AUTO modes |
| 227 | + switch (controlSequence) { |
| 228 | + case THERMOSTAT_SEQ_OP_COOLING: |
| 229 | + case THERMOSTAT_SEQ_OP_COOLING_REHEAT: |
| 230 | + if (_mode == THERMOSTAT_MODE_HEAT || _mode == THERMOSTAT_MODE_AUTO) { |
| 231 | + break; |
| 232 | + } |
| 233 | + log_e("Invalid Thermostat Mode for Cooling Control Sequence of Operation."); |
| 234 | + return false; |
| 235 | + case THERMOSTAT_SEQ_OP_HEATING: |
| 236 | + case THERMOSTAT_SEQ_OP_HEATING_REHEAT: |
| 237 | + if (_mode == THERMOSTAT_MODE_COOL || _mode == THERMOSTAT_MODE_AUTO) { |
| 238 | + break; |
| 239 | + } |
| 240 | + log_e("Invalid Thermostat Mode for Heating Control Sequence of Operation."); |
| 241 | + return false; |
| 242 | + default: |
| 243 | + // compiler warning about not handling all enum values |
| 244 | + break; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + // avoid processing if there was no change |
| 249 | + if (currentMode == _mode) { |
| 250 | + return true; |
| 251 | + } |
| 252 | + |
| 253 | + esp_matter_attr_val_t modeVal = esp_matter_invalid(NULL); |
| 254 | + if (!getAttributeVal(Thermostat::Id, Thermostat::Attributes::SystemMode::Id, &modeVal)) { |
| 255 | + log_e("Failed to get Thermostat Mode Attribute."); |
| 256 | + return false; |
| 257 | + } |
| 258 | + if (modeVal.val.u8 != _mode) { |
| 259 | + modeVal.val.u8 = _mode; |
| 260 | + bool ret; |
| 261 | + ret = updateAttributeVal(Thermostat::Id, Thermostat::Attributes::SystemMode::Id, &modeVal); |
| 262 | + if (!ret) { |
| 263 | + log_e("Failed to update Thermostat Mode Attribute."); |
| 264 | + return false; |
| 265 | + } |
| 266 | + currentMode = _mode; |
| 267 | + } |
| 268 | + log_v("Thermostat Mode set to %d", _mode); |
| 269 | + |
| 270 | + return true; |
| 271 | +} |
| 272 | + |
| 273 | +bool MatterThermostat::setRawTemperature(int16_t _rawTemperature, uint32_t attribute_id, int16_t *internalValue) { |
| 274 | + if (!started) { |
| 275 | + log_e("Matter Thermostat device has not begun."); |
| 276 | + return false; |
| 277 | + } |
| 278 | + |
| 279 | + // avoid processing if there was no change |
| 280 | + if (*internalValue == _rawTemperature) { |
| 281 | + return true; |
| 282 | + } |
| 283 | + |
| 284 | + esp_matter_attr_val_t temperatureVal = esp_matter_invalid(NULL); |
| 285 | + if (!getAttributeVal(Thermostat::Id, attribute_id, &temperatureVal)) { |
| 286 | + log_e("Failed to get Thermostat Temperature or Setpoint Attribute."); |
| 287 | + return false; |
| 288 | + } |
| 289 | + if (temperatureVal.val.i16 != _rawTemperature) { |
| 290 | + temperatureVal.val.i16 = _rawTemperature; |
| 291 | + bool ret; |
| 292 | + ret = updateAttributeVal(Thermostat::Id, attribute_id, &temperatureVal); |
| 293 | + if (!ret) { |
| 294 | + log_e("Failed to update Thermostat Temperature or Setpoint Attribute."); |
| 295 | + return false; |
| 296 | + } |
| 297 | + *internalValue = _rawTemperature; |
| 298 | + } |
| 299 | + log_v("Temperature set to %.01fC", (float)_rawTemperature / 100.00); |
| 300 | + |
| 301 | + return true; |
| 302 | +} |
| 303 | + |
| 304 | +bool MatterThermostat::setCoolingHeatingSetpoints(double _setpointHeatingTemperature, double _setpointCollingTemperature) { |
| 305 | + // at least one of the setpoints must be valid |
| 306 | + bool settingCooling = _setpointCollingTemperature != (float)0xffff; |
| 307 | + bool settingHeating = _setpointHeatingTemperature != (float)0xffff; |
| 308 | + if (!settingCooling && !settingHeating) { |
| 309 | + log_e("Invalid Setpoints values. Set correctly at least one of them in Celsius."); |
| 310 | + return false; |
| 311 | + } |
| 312 | + int16_t _rawHeatValue = static_cast<int16_t>(_setpointHeatingTemperature * 100.0f); |
| 313 | + int16_t _rawCoolValue = static_cast<int16_t>(_setpointCollingTemperature * 100.0f); |
| 314 | + |
| 315 | + // check limits for the setpoints |
| 316 | + if (settingHeating && (_rawHeatValue < kDefaultMinHeatSetpointLimit || _rawHeatValue > kDefaultMaxHeatSetpointLimit)) { |
| 317 | + log_e( |
| 318 | + "Invalid Heating Setpoint value: %.01fC - valid range %d..%d", _setpointHeatingTemperature, kDefaultMinHeatSetpointLimit / 100, |
| 319 | + kDefaultMaxHeatSetpointLimit / 100 |
| 320 | + ); |
| 321 | + return false; |
| 322 | + } |
| 323 | + if (settingCooling && (_rawCoolValue < kDefaultMinCoolSetpointLimit || _rawCoolValue > kDefaultMaxCoolSetpointLimit)) { |
| 324 | + log_e( |
| 325 | + "Invalid Cooling Setpoint value: %.01fC - valid range %d..%d", _setpointCollingTemperature, kDefaultMinCoolSetpointLimit / 100, |
| 326 | + kDefaultMaxCoolSetpointLimit / 100 |
| 327 | + ); |
| 328 | + return false; |
| 329 | + } |
| 330 | + |
| 331 | + // AUTO mode requires both setpoints to be valid to each other and respect the deadband |
| 332 | + if (currentMode == THERMOSTAT_MODE_AUTO) { |
| 333 | +#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR |
| 334 | + float deadband = getDeadBand(); |
| 335 | +#endif |
| 336 | + // only setting Cooling Setpoint |
| 337 | + if (settingCooling && !settingHeating && _rawCoolValue < (heatingSetpointTemperature + (kDefaultDeadBand * 10))) { |
| 338 | + log_e( |
| 339 | + "AutoMode :: Invalid Cooling Setpoint value: %.01fC - must be higher or equal than %.01fC", _setpointCollingTemperature, getHeatingSetpoint() + deadband |
| 340 | + ); |
| 341 | + return false; |
| 342 | + } |
| 343 | + // only setting Heating Setpoint |
| 344 | + if (!settingCooling && settingHeating && _rawHeatValue > (coolingSetpointTemperature - (kDefaultDeadBand * 10))) { |
| 345 | + log_e( |
| 346 | + "AutoMode :: Invalid Heating Setpoint value: %.01fC - must be lower or equal than %.01fC", _setpointHeatingTemperature, getCoolingSetpoint() - deadband |
| 347 | + ); |
| 348 | + return false; |
| 349 | + } |
| 350 | + // setting both setpoints |
| 351 | + if (settingCooling && settingHeating && (_rawCoolValue <= _rawHeatValue || _rawCoolValue - _rawHeatValue < kDefaultDeadBand * 10.0)) { |
| 352 | + log_e( |
| 353 | + "AutoMode :: Error - Heating Setpoint %.01fC must be lower than Cooling Setpoint %.01fC with a minimum difference of %0.1fC", |
| 354 | + _setpointHeatingTemperature, _setpointCollingTemperature, deadband |
| 355 | + ); |
| 356 | + return false; |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + bool ret = true; |
| 361 | + if (settingCooling) { |
| 362 | + ret &= setRawTemperature(_rawCoolValue, Thermostat::Attributes::OccupiedCoolingSetpoint::Id, &coolingSetpointTemperature); |
| 363 | + } |
| 364 | + if (settingHeating) { |
| 365 | + ret &= setRawTemperature(_rawHeatValue, Thermostat::Attributes::OccupiedHeatingSetpoint::Id, &heatingSetpointTemperature); |
| 366 | + } |
| 367 | + return ret; |
| 368 | +} |
| 369 | + |
| 370 | +#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ |
0 commit comments