Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multiple pins #147

Merged
merged 30 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ resources:

| Name | Type | Requirement | Description |
| ----------------- | ------ | ------------ | ------------------------------------------------------------------------------------------------------------------------- |
| code | string | **Required** | Pin code the user needs to enter to unlock |
| code | string/list | **Required** | Pin code the user needs to enter to unlock. Could be a list of codes |
| text | string | **Optional** | Text to display in prompt dialog |
| exemptions | list | **Optional** | List of exemption objects. See [Exemption Options](#exemption-options). |
| condition | map | **Optional** | Conditional object to make restriction active. See [Condition Options](#condition-options). |
Expand Down Expand Up @@ -250,6 +250,26 @@ Theme file:
restriction-overlay-row-outline-blocked: 1px solid rgba(127,127,127,0.1)
```

Multiple pin codes example
```
...
restrictions:
pin:
code:
- abc1234
- 1234
- "0000"
- 5656
- 12
- "0012"
text: Enter pin to unlock
...
```
Notes:
1. Numerical values with leading zeros may be mistreated. To avoid this, wrap values in quotes.
2. Dependently on a presence of alpha-numeric pin codes (like `abcd`, `abcd1234`, `12 34`, `12.24`, `12,34`) in the `code` option, a particular "enter pin" dialog is shown: if all values are numerical - a numerical keypad is shown, otherwise - a simple input-box allowing to input any characters.

<hr style="border: 1px soild gray">

Special Consideration for Input Selects:

Expand Down
2 changes: 1 addition & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const CARD_VERSION = '1.2.16';
export const CARD_VERSION = '1.2.17';
25 changes: 22 additions & 3 deletions src/restriction-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,35 @@ class RestrictionCard extends LitElement implements LovelaceCard {
}

if (this._config.restrictions.pin && this._matchRestriction(this._config.restrictions.pin)) {
const isMultiplePins = Array.isArray(this._config.restrictions.pin.code);
const regex = /^\d+$/;
const codeFormat = regex.test(this._config.restrictions.pin.code) ? 'number' : 'text';
let codeFormat;
if (!isMultiplePins) {
const asString = this._config.restrictions.pin.code as string;
codeFormat = regex.test(asString) ? 'number' : 'text';
} else {
const asArray = this._config.restrictions.pin.code as string[];
codeFormat = regex.test(asArray.join('')) ? 'number' : 'text';
}
const pin = await this._helpers.showEnterCodeDialog(lock, {
codeFormat: codeFormat,
title: this._config.restrictions.pin.text || 'Input pin code',
submitText: 'OK',
});

// tslint:disable-next-line: triple-equals
if (pin != this._config.restrictions.pin.code) {
let conditionString = false;
if (!isMultiplePins) conditionString = pin != (this._config.restrictions.pin.code as string);

let conditionArray = false;
if (isMultiplePins)
for (const pinElement of this._config.restrictions.pin.code) {
if (String(pinElement) === pin) {
conditionArray = false;
break;
} else conditionArray = true;
}

if (conditionString || conditionArray) {
lock.classList.add('invalid');
this._delay = Boolean(this._config.restrictions.pin.retry_delay);
if (this._config.restrictions.pin.max_retries) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface HideRestrictionConfig {
}

export interface PinRestrictionConfig {
code: string;
code: string | string[];
text?: string;
exemptions?: ExemptionConfig[];
condition?: ConditionConfig;
Expand Down
Loading