Skip to content

Commit

Permalink
Abort set up process when provider is already configured
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinfrlch committed May 21, 2024
1 parent ffdab1b commit 54fa941
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions custom_components/gpt4vision/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def validate_localai(user_input: dict):
if not user_input[CONF_PORT]:
raise ServiceValidationError("empty_port")
# perform handshake with LocalAI server
if not handshake(user_input[CONF_IP_ADDRESS], user_input[CONF_PORT]):
if not validate_connection(user_input[CONF_IP_ADDRESS], user_input[CONF_PORT]):
raise ServiceValidationError("handshake_failed")


Expand All @@ -34,7 +34,7 @@ def validate_openai(user_input: dict):
raise ServiceValidationError("empty_api_key")


def handshake(ip_address, port):
def validate_connection(ip_address, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # One second timeout
try:
Expand Down Expand Up @@ -64,8 +64,12 @@ async def async_step_user(self, user_input=None):
if user_input is not None:
self.init_info = user_input
if user_input["provider"] == "LocalAI":
if DOMAIN in self.hass.data and CONF_IP_ADDRESS in self.hass.data[DOMAIN] and CONF_PORT in self.hass.data[DOMAIN]:
return self.async_abort(reason="already_configured")
return await self.async_step_localai()
else:
if DOMAIN in self.hass.data and CONF_OPENAI_API_KEY in self.hass.data[DOMAIN]:
return self.async_abort(reason="already_configured")
return await self.async_step_openai()

return self.async_show_form(
Expand Down
3 changes: 2 additions & 1 deletion custom_components/gpt4vision/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"error": {
"openai_not_configured": "OpenAI provider is not configured",
"localai_not_configured": "LocalAI provider is not configured",
"invalid_image_path": "Image not found. Check the path and try again."
"invalid_image_path": "Image not found. Check the path and try again.",
"already_configured": "Provider is already configured. Delete the existing configuration to add a new one."
}
}
}

0 comments on commit 54fa941

Please sign in to comment.