From 0cf2ac2c68189f24a9d89782ac299edca87aec6e Mon Sep 17 00:00:00 2001 From: psyko_chewbacca Date: Thu, 22 Aug 2024 22:40:57 -0400 Subject: [PATCH] Ability to use strings stored in flash. Saves a bit of memory depending on how long these fields are. No breaking change. --- src/HADevice.cpp | 33 +++++++++++++++++++++++++++++++++ src/HADevice.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/HADevice.cpp b/src/HADevice.cpp index bdf47760..bb108ba0 100644 --- a/src/HADevice.cpp +++ b/src/HADevice.cpp @@ -64,16 +64,31 @@ void HADevice::setManufacturer(const char* manufacturer) _serializer->set(AHATOFSTR(HADeviceManufacturerProperty), manufacturer); } +void HADevice::setManufacturer(const __FlashStringHelper* manufacturer) +{ + _serializer->set(AHATOFSTR(HADeviceManufacturerProperty), manufacturer, HASerializer::ProgmemPropertyValue); +} + void HADevice::setModel(const char* model) { _serializer->set(AHATOFSTR(HADeviceModelProperty), model); } +void HADevice::setModel(const __FlashStringHelper* model) +{ + _serializer->set(AHATOFSTR(HADeviceModelProperty), model, HASerializer::ProgmemPropertyValue); +} + void HADevice::setName(const char* name) { _serializer->set(AHATOFSTR(HANameProperty), name); } +void HADevice::setName(const __FlashStringHelper* name) +{ + _serializer->set(AHATOFSTR(HANameProperty), name, HASerializer::ProgmemPropertyValue); +} + void HADevice::setSoftwareVersion(const char* softwareVersion) { _serializer->set( @@ -82,6 +97,15 @@ void HADevice::setSoftwareVersion(const char* softwareVersion) ); } +void HADevice::setSoftwareVersion(const __FlashStringHelper* softwareVersion) +{ + _serializer->set( + AHATOFSTR(HADeviceSoftwareVersionProperty), + softwareVersion, + HASerializer::ProgmemPropertyValue + ); +} + void HADevice::setConfigurationUrl(const char* url) { _serializer->set( @@ -90,6 +114,15 @@ void HADevice::setConfigurationUrl(const char* url) ); } +void HADevice::setConfigurationUrl(const __FlashStringHelper* url) +{ + _serializer->set( + AHATOFSTR(HADeviceConfigurationUrlProperty), + url, + HASerializer::ProgmemPropertyValue + ); +} + void HADevice::setAvailability(bool online) { _available = online; diff --git a/src/HADevice.h b/src/HADevice.h index 09499827..d2a6be07 100644 --- a/src/HADevice.h +++ b/src/HADevice.h @@ -103,6 +103,13 @@ class HADevice */ void setManufacturer(const char* manufacturer); + /** + * Sets the "manufacturer" property that's going to be displayed in the Home Assistant. + * + * @param manufacturer Any string. Data must reside in flash. Use PROGMEM or 'F()' macro. + */ + void setManufacturer(const __FlashStringHelper* manufacturer); + /** * Sets the "model" property that's going to be displayed in the Home Assistant. * @@ -110,6 +117,13 @@ class HADevice */ void setModel(const char* model); + /** + * Sets the "model" property that's going to be displayed in the Home Assistant. + * + * @param model Any string. Data must reside in flash. Use PROGMEM or 'F()' macro. + */ + void setModel(const __FlashStringHelper* model); + /** * Sets the "name" property that's going to be displayed in the Home Assistant. * @@ -117,6 +131,13 @@ class HADevice */ void setName(const char* name); + /** + * Sets the "name" property that's going to be displayed in the Home Assistant. + * + * @param name Any string. Data must reside in flash. Use PROGMEM or 'F()' macro. + */ + void setName(const __FlashStringHelper* name); + /** * Sets the "software version" property that's going to be displayed in the Home Assistant. * @@ -124,6 +145,13 @@ class HADevice */ void setSoftwareVersion(const char* softwareVersion); + /** + * Sets the "software version" property that's going to be displayed in the Home Assistant. + * + * @param softwareVersion Any string. Data must reside in flash. Use PROGMEM or 'F()' macro. + */ + void setSoftwareVersion(const __FlashStringHelper* softwareVersion); + /** * Sets the "configuration URL" property that's going to be used by the Home Assistant. * @@ -131,6 +159,13 @@ class HADevice */ void setConfigurationUrl(const char* url); + /** + * Sets the "configuration URL" property that's going to be used by the Home Assistant. + * + * @param url Configuration URL to publish. Data must reside in flash. Use PROGMEM or 'F()' macro. + */ + void setConfigurationUrl(const __FlashStringHelper* url); + /** * Sets device's availability and publishes MQTT message on the availability topic. * If the device is not connected to an MQTT broker or the shared availability is not enabled then nothing happens.