I am writing a custom component to access the ESP32 nvs flash partition. I have the initialization working pretty much as I want, but I’d like the validation to verify that the ‘key’ is 15 characters or less in length to meet the esp idf nvs library requirements. I’m using the id:
yaml setting as the key. The yaml to use the new component looks like this:
esp32nvs:
- id: test_nvs_key
type: int
nvs_namespace: 'test_namespace'
initial_value: 100
- id: test_nvs_string
type: String
nvs_namespace: 'test_namespace'
initial_value: "Hello"
The __init__.py section looks like this:
def validate_type(value):
if str(value) != 'String' and str(value) != 'int':
raise cv.Invalid("type: must be String (capitalized) or int")
return value
def validate_id(value):
#if len(str(value)) < 1:
# raise cv.Invalid("id must be at least 1 character long")
#if len(str(value)) > 15:
# raise cv.Invalid("id must be at most 15 characters long")
return cv.declare_id(Esp32NvsComponent)
MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema({
#cv.Required(CONF_ID): validate_id,
cv.Required(CONF_ID): cv.declare_id(Esp32NvsComponent),
cv.Required(CONF_TYPE): validate_type,
cv.Required(CONF_NVS_NAMESPACE): cv.All(cv.string, cv.Length(max=15), cv.Length(min=1)),
cv.Required(CONF_INITIAL_VALUE): cv.Any(cv.string_strict, cv.int_),
}).extend(cv.COMPONENT_SCHEMA)
I have not been able to find a way to verify that the CONF_ID
is 15 characters or less. The above code validates OK, but allows id:
strings that are too long. If I just swap the comments on the cv.Required(CONF_ID)
lines so it uses validate_id
(with all the length checked commented out, as shown) instead of the direct cv.declare_id(Esp32NvsComponent)
, I get this error on validation:
Couldn't find ID 'test_nvs_key'. Please check you have defined an ID with that name in your configuration.
But even if calling validate_id
worked, I don’t think that method of verifying the string length works.
Anybody know of a way?