Skip to content

Commit

Permalink
Merge pull request #74 from ufozone/73-generate-new-voucher-if-no-one…
Browse files Browse the repository at this point in the history
…-is-available

Add voucher creation if no one is available
  • Loading branch information
ufozone committed May 24, 2024
2 parents 459d259 + e3ee59b commit 08f4d27
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
11 changes: 11 additions & 0 deletions custom_components/unifi_voucher/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
CONF_VOUCHER_USAGE_QUOTA,
CONF_VOUCHER_RATE_MAX_UP,
CONF_VOUCHER_RATE_MAX_DOWN,
CONF_CREATE_IF_NONE_EXISTS,
)
from .api import (
UnifiVoucherApiClient,
Expand Down Expand Up @@ -275,6 +276,7 @@ async def async_step_options(
CONF_VOUCHER_USAGE_QUOTA: _set_option(user_input, CONF_VOUCHER_USAGE_QUOTA),
CONF_VOUCHER_RATE_MAX_UP: _set_option(user_input, CONF_VOUCHER_RATE_MAX_UP),
CONF_VOUCHER_RATE_MAX_DOWN: _set_option(user_input, CONF_VOUCHER_RATE_MAX_DOWN),
CONF_CREATE_IF_NONE_EXISTS: user_input.get(CONF_CREATE_IF_NONE_EXISTS, False),
}
)
# User is done, create the config entry.
Expand Down Expand Up @@ -404,6 +406,10 @@ async def async_step_options(
unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
)
),
vol.Optional(
CONF_CREATE_IF_NONE_EXISTS,
default=(user_input or {}).get(CONF_CREATE_IF_NONE_EXISTS, False),
): selector.BooleanSelector(),
}
),
last_step=True,
Expand Down Expand Up @@ -495,6 +501,7 @@ async def async_step_init(
CONF_VOUCHER_USAGE_QUOTA: _set_option(user_input, CONF_VOUCHER_USAGE_QUOTA),
CONF_VOUCHER_RATE_MAX_UP: _set_option(user_input, CONF_VOUCHER_RATE_MAX_UP),
CONF_VOUCHER_RATE_MAX_DOWN: _set_option(user_input, CONF_VOUCHER_RATE_MAX_DOWN),
CONF_CREATE_IF_NONE_EXISTS: user_input.get(CONF_CREATE_IF_NONE_EXISTS, False),
}
)
# User is done, update the config entry.
Expand Down Expand Up @@ -604,6 +611,10 @@ async def async_step_init(
unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
)
),
vol.Optional(
CONF_CREATE_IF_NONE_EXISTS,
default=(user_input or self.options or {}).get(CONF_CREATE_IF_NONE_EXISTS, False),
): selector.BooleanSelector(),
}
),
last_step=True,
Expand Down
2 changes: 2 additions & 0 deletions custom_components/unifi_voucher/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
CONF_VOUCHER_USAGE_QUOTA = "voucher_usage_quota"
CONF_VOUCHER_RATE_MAX_UP = "voucher_rate_max_up"
CONF_VOUCHER_RATE_MAX_DOWN = "voucher_rate_max_down"
CONF_CREATE_IF_NONE_EXISTS = "create_if_none_exists"

ATTR_EXTRA_STATE_ATTRIBUTES = "extra_state_attributes"
ATTR_LAST_PULL = "last_pull"
ATTR_AVAILABLE = "available"
ATTR_VOUCHER = "voucher"
ATTR_QR_CODE = "qr_code"

DEFAULT_IDENTIFIER_STRING = "HA-generated"
DEFAULT_SITE_ID = "default"
DEFAULT_HOST = ""
DEFAULT_USERNAME = ""
Expand Down
12 changes: 10 additions & 2 deletions custom_components/unifi_voucher/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
CONF_VOUCHER_USAGE_QUOTA,
CONF_VOUCHER_RATE_MAX_UP,
CONF_VOUCHER_RATE_MAX_DOWN,
CONF_CREATE_IF_NONE_EXISTS,
DEFAULT_IDENTIFIER_STRING,
DEFAULT_VOUCHER,
)
from .api import (
Expand Down Expand Up @@ -195,11 +197,12 @@ async def async_fetch_vouchers(

vouchers = Vouchers(self.client.controller)
await vouchers.update()

self._last_pull = dt_util.now()
self._available = True
for voucher in vouchers.values():
# No HA generated voucher
if not voucher.note.startswith("HA-generated"):
if not voucher.note.startswith(DEFAULT_IDENTIFIER_STRING):
continue
# Voucher is full used
if voucher.quota > 0 and voucher.quota <= voucher.used:
Expand Down Expand Up @@ -233,6 +236,11 @@ async def async_fetch_vouchers(
self.vouchers = _vouchers
self.latest_voucher_id = _latest_voucher_id

# If no voucher found, create a new one
if _latest_voucher_id is None and self.config_entry.options.get(CONF_CREATE_IF_NONE_EXISTS, False):
LOGGER.info("No voucher found, create a new one")
await self.async_create_voucher()

async def async_create_voucher(
self,
number: int | None = None,
Expand Down Expand Up @@ -271,7 +279,7 @@ async def async_create_voucher(
usage_quota=usage_quota,
rate_max_up=rate_max_up,
rate_max_down=rate_max_down,
note="HA-generated",
note=DEFAULT_IDENTIFIER_STRING,
)
)
await self.async_update_vouchers()
Expand Down
1 change: 1 addition & 0 deletions custom_components/unifi_voucher/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def image(self) -> bytes | None:
self.cached_image = wlan_qr_code(
name=self.current_wlan_name,
password=None,
kind="png",
)
return self.cached_image

Expand Down
6 changes: 4 additions & 2 deletions custom_components/unifi_voucher/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"voucher_duration": "How long should a voucher be valid from the time it is used?",
"voucher_usage_quota": "How much data traffic should be available per voucher? (0 = unlimited)",
"voucher_rate_max_up": "How much upload bandwidth should be available per voucher? (0 = unlimited)",
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)"
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)",
"create_if_none_exists": "Should new vouchers be created if no more are available?"
}
}
},
Expand Down Expand Up @@ -60,7 +61,8 @@
"voucher_duration": "How long should a voucher be valid from the time it is used?",
"voucher_usage_quota": "How much data traffic should be available per voucher? (0 = unlimited)",
"voucher_rate_max_up": "How much upload bandwidth should be available per voucher? (0 = unlimited)",
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)"
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)",
"create_if_none_exists": "Should new vouchers be created if no more are available?"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions custom_components/unifi_voucher/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"voucher_duration": "Wie lange soll ein Gutschein ab Einlösen gültig sein?",
"voucher_usage_quota": "Wie viel Datenverkehr soll pro Gutschein nutzbar sein? (0 = unbegrenzt)",
"voucher_rate_max_up": "Wie viel Upload-Bandbreite soll pro Gutschein nutzbar sein? (0 = unbegrenzt)",
"voucher_rate_max_down": "Wie viel Download-Bandbreite soll pro Gutschein nutzbar sein? (0 = unbegrenzt)"
"voucher_rate_max_down": "Wie viel Download-Bandbreite soll pro Gutschein nutzbar sein? (0 = unbegrenzt)",
"create_if_none_exists": "Sollen neue Gutscheine erstellt werden, wenn keine verfügbar sind?"
}
}
},
Expand Down Expand Up @@ -60,7 +61,8 @@
"voucher_duration": "Wie lange soll ein Gutschein ab Einlösen gültig sein?",
"voucher_usage_quota": "Wie viel Datenverkehr soll pro Gutschein nutzbar sein? (0 = unbegrenzt)",
"voucher_rate_max_up": "Wie viel Upload-Bandbreite soll pro Gutschein nutzbar sein? (0 = unbegrenzt)",
"voucher_rate_max_down": "Wie viel Download-Bandbreite soll pro Gutschein nutzbar sein? (0 = unbegrenzt)"
"voucher_rate_max_down": "Wie viel Download-Bandbreite soll pro Gutschein nutzbar sein? (0 = unbegrenzt)",
"create_if_none_exists": "Sollen neue Gutscheine erstellt werden, wenn keine verfügbar sind?"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions custom_components/unifi_voucher/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"voucher_duration": "How long should a voucher be valid from the time it is used?",
"voucher_usage_quota": "How much data traffic should be available per voucher? (0 = unlimited)",
"voucher_rate_max_up": "How much upload bandwidth should be available per voucher? (0 = unlimited)",
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)"
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)",
"create_if_none_exists": "Should new vouchers be created if no more are available?"
}
}
},
Expand Down Expand Up @@ -60,7 +61,8 @@
"voucher_duration": "How long should a voucher be valid from the time it is used?",
"voucher_usage_quota": "How much data traffic should be available per voucher? (0 = unlimited)",
"voucher_rate_max_up": "How much upload bandwidth should be available per voucher? (0 = unlimited)",
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)"
"voucher_rate_max_down": "How much download bandwidth should be available per voucher? (0 = unlimited)",
"create_if_none_exists": "Should new vouchers be created if no more are available?"
}
}
}
Expand Down

0 comments on commit 08f4d27

Please sign in to comment.