Skip to content

Commit

Permalink
Added API key to configuration options (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
swartjean committed Nov 27, 2022
1 parent 5aa8850 commit dd9e164
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
3 changes: 2 additions & 1 deletion custom_components/eskom_loadshedding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .const import (
CONF_SCAN_PERIOD,
CONF_API_KEY,
DEFAULT_SCAN_PERIOD,
DOMAIN,
PLATFORMS,
Expand All @@ -42,7 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
)

# Fetch the configured API key and area ID and create the client
api_key = entry.data.get("api_key")
api_key = entry.options.get(CONF_API_KEY, entry.data.get("api_key"))
area_id = entry.data.get("area_id")
session = async_get_clientsession(hass)
client = EskomInterface(session=session, api_key=api_key, area_id=area_id)
Expand Down
58 changes: 48 additions & 10 deletions custom_components/eskom_loadshedding/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .const import ( # pylint: disable=unused-import
CONF_SCAN_PERIOD,
CONF_API_KEY,
DEFAULT_SCAN_PERIOD,
DOMAIN,
MIN_SCAN_PERIOD,
Expand All @@ -32,10 +33,10 @@ async def async_step_user(self, user_input=None):

if user_input is not None:
# Validate the API key passed in by the user
valid = await self.validate_key(user_input["api_key"])
valid = await self.validate_key(user_input[CONF_API_KEY])
if valid:
# Store info to use in next step
self.api_key = user_input["api_key"]
self.api_key = user_input[CONF_API_KEY]

# Proceed to the next configuration step
return await self.async_step_area_search()
Expand All @@ -46,7 +47,7 @@ async def async_step_user(self, user_input=None):
return await self._show_user_config_form(user_input)

user_input = {}
user_input["api_key"] = ""
user_input[CONF_API_KEY] = ""

return await self._show_user_config_form(user_input)

Expand Down Expand Up @@ -84,9 +85,11 @@ async def async_step_area_selection(self, user_input=None):
return self.async_create_entry(
title="Loadshedding Status",
data={
"api_key": self.api_key,
"area_id": user_input["area_selection"],
},
options={
CONF_API_KEY: self.api_key,
},
)
else:
self._errors["base"] = "no_area_selection"
Expand Down Expand Up @@ -114,7 +117,9 @@ def async_get_options_flow(config_entry):

async def _show_user_config_form(self, user_input):
"""Show the configuration form."""
data_schema = {vol.Required("api_key", default=user_input["api_key"]): str}
data_schema = {
vol.Required(CONF_API_KEY, default=user_input[CONF_API_KEY]): str
}

return self.async_show_form(
step_id="user", data_schema=vol.Schema(data_schema), errors=self._errors
Expand Down Expand Up @@ -159,6 +164,7 @@ class EskomOptionsFlowHandler(config_entries.OptionsFlow):

def __init__(self, config_entry):
"""Initialize HACS options flow."""
self._errors = {}
self.config_entry = config_entry
self.options = dict(config_entry.options)

Expand All @@ -168,14 +174,21 @@ async def async_step_init(self, user_input=None): # pylint: disable=unused-argu

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
self._errors = {}

if user_input is not None:
# Set a minimum scan period
if int(user_input[CONF_SCAN_PERIOD]) >= MIN_SCAN_PERIOD:
# Validate the API key
valid = await self.validate_key(user_input[CONF_API_KEY])
if valid:
# Set a minimum scan period
if int(user_input[CONF_SCAN_PERIOD]) < MIN_SCAN_PERIOD:
user_input[CONF_SCAN_PERIOD] = MIN_SCAN_PERIOD

# Update all options
self.options.update(user_input)
return await self._update_options()
else:
self.options[CONF_SCAN_PERIOD] = MIN_SCAN_PERIOD
return await self._update_options()
self._errors["base"] = "auth"

data_schema = OrderedDict()
data_schema[
Expand All @@ -185,11 +198,36 @@ async def async_step_user(self, user_input=None):
)
] = int

data_schema[
vol.Optional(
CONF_API_KEY,
default=self.options.get(CONF_API_KEY, self.options.get(CONF_API_KEY)),
)
] = str

for x in sorted(PLATFORMS):
data_schema[vol.Required(x, default=self.options.get(x, True))] = bool

return self.async_show_form(step_id="user", data_schema=vol.Schema(data_schema))
return self.async_show_form(
step_id="user", data_schema=vol.Schema(data_schema), errors=self._errors
)

async def _update_options(self):
"""Update config entry options."""
return self.async_create_entry(title="Home", data=self.options)

async def validate_key(self, api_key: str) -> bool:
"""Validates an EskomSePush API token."""
# Perform an api allowance check using the provided token
try:
session = async_create_clientsession(self.hass)
interface = EskomInterface(session=session, api_key=api_key)
data = await interface.async_query_api("/api_allowance")

if "error" in data:
return False
else:
return True
except Exception: # pylint: disable=broad-except
pass
return False
1 change: 1 addition & 0 deletions custom_components/eskom_loadshedding/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Configuration and options
CONF_ENABLED = "enabled"
CONF_SCAN_PERIOD = "scan_period"
CONF_API_KEY = "api_key"

# Defaults
DEFAULT_SCAN_PERIOD = 7200
Expand Down
4 changes: 4 additions & 0 deletions custom_components/eskom_loadshedding/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
}
},
"options": {
"error": {
"auth": "The API key provided is not valid."
},
"step": {
"user": {
"data": {
"api_key": "API Key",
"scan_period": "Scan Period (s)",
"sensor": "Enable Sensors",
"calendar": "Enable Calendars"
Expand Down
4 changes: 4 additions & 0 deletions custom_components/eskom_loadshedding/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
}
},
"options": {
"error": {
"auth": "The API key provided is not valid."
},
"step": {
"user": {
"data": {
"api_key": "API Key",
"scan_period": "Scan Period (s)",
"sensor": "Enable Sensors",
"calendar": "Enable Calendars"
Expand Down

0 comments on commit dd9e164

Please sign in to comment.