Skip to content

Commit 2b90d56

Browse files
filmackaydrc38
andauthored
Improve the validation and user feedback for the charge point identity (#1886)
* Update switch to push updates - not be polled * Update HA states after connection Ensure Home Assistant states are updated immediately after connection. * Change async_write_ha_state to async_schedule_update_ha_state Refactor state update method for switch entity - for consistency. * Formatting update * fix test errors * Improve supported feature coding * add TypeError guard for None * Fix for config flow charger name --------- Co-authored-by: drc38 <20024196+drc38@users.noreply.github.com>
1 parent 2f76280 commit 2b90d56

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

custom_components/ocpp/config_flow.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ConfigFlowResult,
88
CONN_CLASS_LOCAL_PUSH,
99
)
10+
from homeassistant.helpers import config_validation as cv
1011
import voluptuous as vol
1112

1213
from .const import (
@@ -79,10 +80,7 @@
7980

8081
STEP_USER_CP_DATA_SCHEMA = vol.Schema(
8182
{
82-
vol.Required(CONF_CPID, default=DEFAULT_CPID): vol.All(
83-
str,
84-
vol.Match(r"^[a-z0-9_$]+$", msg="Use only lower case, digits and _"),
85-
),
83+
vol.Required(CONF_CPID, default=DEFAULT_CPID): str,
8684
vol.Required(CONF_MAX_CURRENT, default=DEFAULT_MAX_CURRENT): int,
8785
vol.Required(
8886
CONF_MONITORED_VARIABLES_AUTOCONFIG,
@@ -168,26 +166,34 @@ async def async_step_cp_user(
168166
if user_input is not None:
169167
# Don't allow duplicate cpids to be used
170168
self._async_abort_entries_match({CONF_CPID: user_input[CONF_CPID]})
171-
172-
cp_data = {
173-
**user_input,
174-
CONF_NUM_CONNECTORS: self._detected_num_connectors,
175-
}
176-
cpids_list = self._data.get(CONF_CPIDS, []).copy()
177-
cpids_list.append({self._cp_id: cp_data})
178-
self._data = {**self._data, CONF_CPIDS: cpids_list}
179-
180-
if user_input[CONF_MONITORED_VARIABLES_AUTOCONFIG]:
181-
self._data[CONF_CPIDS][-1][self._cp_id][CONF_MONITORED_VARIABLES] = (
182-
DEFAULT_MONITORED_VARIABLES
183-
)
184-
self.hass.config_entries.async_update_entry(
185-
self._entry, data=self._data
186-
)
187-
return self.async_abort(reason="Added/Updated charge point")
188-
169+
# Validate cpid format against entity id requirements (lowercase letters, digits and _)
170+
schema = vol.Schema(
171+
{vol.Required(CONF_CPID): cv.matches_regex(r"^[\da-z_]+$")}
172+
)
173+
try:
174+
schema({CONF_CPID: user_input[CONF_CPID]})
175+
except vol.Invalid:
176+
errors["base"] = "invalid_cpid"
189177
else:
190-
return await self.async_step_measurands()
178+
cp_data = {
179+
**user_input,
180+
CONF_NUM_CONNECTORS: self._detected_num_connectors,
181+
}
182+
cpids_list = self._data.get(CONF_CPIDS, []).copy()
183+
cpids_list.append({self._cp_id: cp_data})
184+
self._data = {**self._data, CONF_CPIDS: cpids_list}
185+
186+
if user_input[CONF_MONITORED_VARIABLES_AUTOCONFIG]:
187+
self._data[CONF_CPIDS][-1][self._cp_id][
188+
CONF_MONITORED_VARIABLES
189+
] = DEFAULT_MONITORED_VARIABLES
190+
self.hass.config_entries.async_update_entry(
191+
self._entry, data=self._data
192+
)
193+
return self.async_abort(reason="Added/Updated charge point")
194+
195+
else:
196+
return await self.async_step_measurands()
191197

192198
return self.async_show_form(
193199
step_id="cp_user",

custom_components/ocpp/translations/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
},
6262
"error": {
6363
"auth": "Username/Password is wrong.",
64-
"no_measurands_selected": "No measurand selected: please select at least one"
64+
"no_measurands_selected": "No measurand selected: please select at least one",
65+
"invalid_cpid": "Invalid charge point identity name: use only lower case letters, digits and _"
6566
},
6667
"abort": {
6768
"single_instance_allowed": "Only a single instance is allowed",

custom_components/ocpp/translations/i-default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
},
6262
"error": {
6363
"auth": "Username/Password is wrong.",
64-
"no_measurands_selected": "No measurand selected: please select at least one"
64+
"no_measurands_selected": "No measurand selected: please select at least one",
65+
"invalid_cpid": "Invalid charge point identity name: use only lower case letters, digits and _"
6566
},
6667
"abort": {
6768
"single_instance_allowed": "Only a single instance is allowed",

0 commit comments

Comments
 (0)