I’m the creator of the Visonic Alarm Integration in HACS and I’m trying to update the Configuration settings to make it more clear and robust.
I currently have this for setting the override code in the Configuration.
import voluptuous as vol
from homeassistant.helpers import config_validation as cv
CONF_OVERRIDE_CODE = "override_code"
.........
vol.Optional(
CONF_OVERRIDE_CODE, default=self.create_default(options, CONF_OVERRIDE_CODE, "")
): cv.string,
The self.create_default
function looks up the setting from last time and fills it in as the default for the text box, if it’s not there then it sets it to the empty string.
It almost works in that it displays a text box in the form to enter a text string. There are 2 problems with this:
- There is no validation that the user has to enter a 4 digit numeric code
- Once the user enters a code, there is no way to delete it and empty the text box. I think this is a bug in Home Assistant somewhere. On a Configuration Form the user should be able to delete the content of the default and empty the text box, the form should return an empty string but it returns the default value.
In any case, I tried to update it using a regex validation like this to get it to be more robust:
vol.Optional(
CONF_OVERRIDE_CODE, default=self.create_default(options, CONF_OVERRIDE_CODE, "")
): cv.matches_regex("(^[0-9][0-9][0-9][0-9]$|^$)"),
I’m assuming that cv.matches_regex will create a text box in the form in the same way as cv.string but I haven’t yet got it to work so I’m not sure.
This regex should allow the user to enter the empty string or a 4 digit code but it crashes with the following error:
2022-08-13 11:47:47.424 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
resp = await request_handler(request)
File "/usr/local/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
resp = await handler(request)
File "/usr/local/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
return await handler(request)
File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
return await handler(request)
File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 94, in forwarded_middleware
return await handler(request)
File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
return await handler(request)
File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 82, in ban_middleware
return await handler(request)
File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
return await handler(request)
File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 136, in handle
result = await result
File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 215, in post
return await super().post(request)
File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 73, in wrapper
result = await method(view, request, data, *args, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 84, in post
result = self._prepare_result_json(result)
File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 43, in _prepare_result_json
data["data_schema"] = voluptuous_serialize.convert(
File "/usr/local/lib/python3.10/site-packages/voluptuous_serialize/__init__.py", line 40, in convert
pval = convert(value, custom_serializer=custom_serializer)
File "/usr/local/lib/python3.10/site-packages/voluptuous_serialize/__init__.py", line 113, in convert
if issubclass(schema, Enum):
TypeError: issubclass() arg 1 must be a class
I have changed the regex but it doesn’t make a difference.
Please help, what am I doing wrong?