From e9bfd09545037f3289f82d96a8c65b03d0af7326 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:35:25 +0100 Subject: [PATCH] Document as_int in NumberSelector --- blog/2024-11-08-number_selector.md | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 blog/2024-11-08-number_selector.md diff --git a/blog/2024-11-08-number_selector.md b/blog/2024-11-08-number_selector.md new file mode 100644 index 00000000..055e9b7f --- /dev/null +++ b/blog/2024-11-08-number_selector.md @@ -0,0 +1,40 @@ +--- +author: epenet +authorURL: https://github.com/epenet +title: "Return an integer in number selector" +--- + +The [Number selector](https://www.home-assistant.io/docs/blueprint/selectors/#number-selector) has been expanded and now also includes an `as_int` parameter. + +Using this in [config flows](/docs/data_entry_flow_index#show-form) will remove the need to add an extra validation to the schema. + +Example: + +```python +vol.Schema( + { + vol.Optional(CONF_ADDRESS): NumberSelector( + NumberSelectorConfig( + as_int=True, min=1, max=255, mode=NumberSelectorMode.BOX + ) + ), + } +) +``` + +Old code: + +```python +vol.Schema( + { + vol.Optional(CONF_ADDRESS): vol.All( + NumberSelector( + NumberSelectorConfig( + min=1, max=255, mode=NumberSelectorMode.BOX + ) + ), + vol.Coerce(int), + ), + } +) +```