Skip to content

Commit

Permalink
Added check for duplicate controller names in config options flows
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Dec 27, 2023
1 parent 666cedb commit e14cd90
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ KeyboardInterrupt
- [x] Select controllers to configure
- [x] Loop until done
- [x] Select doors to configure
- [x] Check for duplicate controller names
- [ ] Ad hoc controllers
- when internal list is empty
- address is required
- [ ] Flatten and validate controllers lists
- [ ] Flatten and validate doors lists
- [ ] Use _self.options_ struct and _self.configured_ list

Expand All @@ -36,6 +36,7 @@ KeyboardInterrupt

- [ ] Options flow
- [ ] Rework to configure multiple controllers, doors, etc
- [x] Check for duplicate controller names
- [ ] show menu
- https://developers.home-assistant.io/docs/data_entry_flow_index/#show-menu

Expand Down
17 changes: 14 additions & 3 deletions custom_components/uhppoted/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import logging

from typing import Any
Expand All @@ -24,9 +25,19 @@
MAX_ERRORS = 5


def validate_controller_id(v: int) -> None:
if not v or v.strip() == '':
raise ValueError
def normalise(v):
re.sub(r'\s+', '', f'{v}', flags=re.UNICODE).lower()


def validate_controller(name, serial_no, address, options) -> None:
if not name or name.strip() == '':
raise ValueError('Blank controller ID')

if options and CONF_CONTROLLERS in options:
for v in options[CONF_CONTROLLERS]:
if normalise(v[CONF_CONTROLLER_ID]) == normalise(name):
if int(f'{v[CONF_CONTROLLER_SERIAL_NUMBER]}') != int(f'{serial_no}'):
raise ValueError('Duplicate controller ID')


def validate_controller_serial_no(v) -> None:
Expand Down
18 changes: 11 additions & 7 deletions custom_components/uhppoted/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

from .options_flow import UhppotedOptionsFlow

from .config import validate_controller_id
from .config import validate_controller
from .config import validate_controller_serial_no
from .config import validate_door_id
from .config import validate_door_controller
Expand Down Expand Up @@ -158,18 +158,22 @@ async def async_step_controller(self, user_input: Optional[Dict[str, Any]] = Non

errors: Dict[str, str] = {}
if user_input is not None:
name = user_input[CONF_CONTROLLER_ID]
serial_no = controller['serial_no']
address = user_input[CONF_CONTROLLER_ADDR]

try:
validate_controller_id(user_input[CONF_CONTROLLER_ID])
except ValueError:
errors["base"] = f'Invalid controller ID ({user_input[CONF_CONTROLLER_ID]})'
validate_controller(name, serial_no, address, self.options)
except ValueError as err:
errors["base"] = f'{err}'

if not errors:
v = self.options[CONF_CONTROLLERS]

v.append({
CONF_CONTROLLER_ID: user_input[CONF_CONTROLLER_ID],
CONF_CONTROLLER_SERIAL_NUMBER: controller['serial_no'],
CONF_CONTROLLER_ADDR: user_input[CONF_CONTROLLER_ADDR],
CONF_CONTROLLER_ID: name,
CONF_CONTROLLER_SERIAL_NUMBER: serial_no,
CONF_CONTROLLER_ADDR: address,
})

self.options.update({CONF_CONTROLLERS: v})
Expand Down
21 changes: 10 additions & 11 deletions custom_components/uhppoted/options_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from .const import CONF_DOOR_CONTROLLER
from .const import CONF_DOOR_NUMBER

from .config import validate_controller_id
from .config import validate_controller
from .config import validate_controller_serial_no
from .config import validate_door_id
from .config import validate_door_controller
Expand Down Expand Up @@ -149,22 +149,21 @@ async def async_step_controller(self, user_input: Optional[Dict[str, Any]] = Non

errors: Dict[str, str] = {}
if user_input is not None:
try:
validate_controller_id(user_input[CONF_CONTROLLER_ID])
except ValueError:
errors["base"] = f'Invalid controller ID ({user_input[CONF_CONTROLLER_ID]})'
name = user_input[CONF_CONTROLLER_ID]
serial_no = user_input[CONF_CONTROLLER_SERIAL_NUMBER]
address = user_input[CONF_CONTROLLER_ADDR]

try:
validate_controller_serial_no(user_input[CONF_CONTROLLER_SERIAL_NUMBER])
except ValueError:
errors["base"] = f'Invalid controller serial number ({user_input[CONF_CONTROLLER_SERIAL_NUMBER]})'
validate_controller(name, serial_no, address, self.options)
except ValueError as err:
errors["base"] = f'{err}'

if not errors:
v = []
v.append({
CONF_CONTROLLER_ID: user_input[CONF_CONTROLLER_ID],
CONF_CONTROLLER_SERIAL_NUMBER: user_input[CONF_CONTROLLER_SERIAL_NUMBER],
CONF_CONTROLLER_ADDR: user_input[CONF_CONTROLLER_ADDR],
CONF_CONTROLLER_ID: name,
CONF_CONTROLLER_SERIAL_NUMBER: serial_no,
CONF_CONTROLLER_ADDR: address,
})

self.options.update({CONF_CONTROLLERS: v})
Expand Down

0 comments on commit e14cd90

Please sign in to comment.