Skip to content

Commit

Permalink
config-flow: retrieving cards from controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Dec 21, 2023
1 parent 1911922 commit be42fc1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
46 changes: 42 additions & 4 deletions custom_components/uhppoted/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from .const import CONF_DOOR_NUMBER

_LOGGER = logging.getLogger(__name__)
MAX_CARDS = 25
MAX_CARD_INDEX = 20000
MAX_ERRORS = 5


def validate_controller_id(v: int) -> None:
Expand Down Expand Up @@ -57,10 +60,6 @@ def validate_card_number(v: int) -> None:
raise ValueError


def list_controllers(options):
return [v[CONF_CONTROLLER_ID] for v in options[CONF_CONTROLLERS]]


def get_all_controllers(options):
controllers = []

Expand All @@ -84,6 +83,45 @@ def get_all_controllers(options):
return controllers


def get_all_cards(options):
cards = set()

bind = options[CONF_BIND_ADDR]
broadcast = options[CONF_BROADCAST_ADDR]
listen = options[CONF_LISTEN_ADDR]
debug = options[CONF_DEBUG]
u = uhppote.Uhppote(bind, broadcast, listen, debug)

controllers = options[CONF_CONTROLLERS]

for c in controllers:
controller = int(f'{c[CONF_CONTROLLER_SERIAL_NUMBER]}'.strip())

try:
response = u.get_cards(controller)
_LOGGER.info(f'{controller}: {response.cards} cards')

N = min(response.cards, MAX_CARDS)
ix = 1
count = 0
errors = 0

while count < N and ix < MAX_CARD_INDEX and len(cards) < MAX_CARDS and errors < MAX_ERRORS:
try:
response = u.get_card_by_index(controller, ix)
count += 1
cards.add(response.card_number)
ix += 1
except Exception as e:
errors += 1
_LOGGER.warning(f'{controller} error retrieving card at index {ix} ({e})')

except Exception as e:
_LOGGER.warning(f'{controller} error retrieving list of cards ({e})')

return sorted(cards)


def configure_controllers(options, f):
controllers = options[CONF_CONTROLLERS]

Expand Down
48 changes: 46 additions & 2 deletions custom_components/uhppoted/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
from .config import validate_door_controller
from .config import validate_door_number
from .config import validate_card_number
from .config import list_controllers
from .config import get_all_controllers
from .config import get_all_cards

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -261,7 +261,7 @@ async def async_step_doors(self, user_input: Optional[Dict[str, Any]] = None):
async def async_step_door(self, user_input: Optional[Dict[str, Any]] = None):
it = next((v for v in self.controllers if not v['doors']['configured']), None)
if it == None:
return await self.async_step_card()
return await self.async_step_cards()

errors: Dict[str, str] = {}
if user_input is not None:
Expand Down Expand Up @@ -350,6 +350,50 @@ async def async_step_door(self, user_input: Optional[Dict[str, Any]] = None):
errors=errors,
description_placeholders=placeholders)

async def async_step_cards(self, user_input: Optional[Dict[str, Any]] = None):
errors: Dict[str, str] = {}

if user_input is not None:
if not errors:
# for v in user_input[CONF_CONTROLLERS]:
# self.controllers.append({
# 'controller': {
# 'name': '',
# 'serial_no': v,
# 'configured': False,
# },
# 'doors': None,
# })
#
# return await self.async_step_controller()
return self.async_create_entry(title="uhppoted", data=self.data, options=self.options)

cards = get_all_cards(self.options)

# if len(cards) < 2:
# for v in controllers:
# self.controllers.append({
# 'controller': {
# 'name': '',
# 'serial_no': v,
# 'configured': False,
# },
# 'doors': None,
# })
#
# return await self.async_step_card()

schema = vol.Schema({
vol.Required(CONF_CARDS, default=[f'{v}' for v in cards]):
SelectSelector(
SelectSelectorConfig(options=[f'{v}' for v in cards],
multiple=True,
custom_value=False,
mode=SelectSelectorMode.LIST)),
})

return self.async_show_form(step_id="cards", data_schema=schema, errors=errors)

async def async_step_card(self, user_input: Optional[Dict[str, Any]] = None):
today = datetime.date.today()
start = today
Expand Down
10 changes: 10 additions & 0 deletions custom_components/uhppoted/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
}
},

"cards": {
"title": "Access cards",
"data": {
"cards": "Select the cards to be configured:"
},
"data_description": {
"cards": "List of cards stored on the controllers"
}
},

"card": {
"title": "Configure access card",
"description": "Creates an access card record",
Expand Down
10 changes: 10 additions & 0 deletions custom_components/uhppoted/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
}
},

"cards": {
"title": "Access cards",
"data": {
"cards": "Select the cards to be configured:"
},
"data_description": {
"cards": "List of cards stored on the controllers"
}
},

"card": {
"title": "Configure access card",
"description": "Creates an access card record",
Expand Down

0 comments on commit be42fc1

Please sign in to comment.