Skip to content

Commit

Permalink
Change host to include the URL scheme (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen committed Jan 9, 2024
1 parent e9e92c8 commit 270bea0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Config is done in the HA integrations UI.

### Host

The IP address of the main deco which you visit in the browser to see the web admin page. Example: `192.168.0.1`
The IP address of the main deco which you visit in the browser to see the web admin page. Example: `http://192.168.0.1`. Some decos use https, so if you have trouble connection, try using `https` e.g. `https://192.168.0.1`.

### Login Credentials

Expand Down
4 changes: 4 additions & 0 deletions custom_components/tplink_deco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
new[CONF_DECO_PREFIX] = ""
new[CONF_DECO_POSTFIX] = DEFAULT_DECO_POSTFIX

if config_entry.version == 5:
config_entry.version = 6
new[CONF_HOST] = f"http://{new[CONF_HOST]}"

hass.config_entries.async_update_entry(config_entry, data=new)

_LOGGER.info("Migration to version %s successful", config_entry.version)
Expand Down
12 changes: 6 additions & 6 deletions custom_components/tplink_deco/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async def _async_list_devices(self) -> dict:
device_list_payload = {"operation": "read"}
response_json = await self._async_post(
context,
f"http://{self._host}/cgi-bin/luci/;stok={self._stok}/admin/device",
f"{self._host}/cgi-bin/luci/;stok={self._stok}/admin/device",
params={"form": "device_list"},
data=self._encode_payload(device_list_payload),
)
Expand Down Expand Up @@ -207,7 +207,7 @@ async def async_reboot_decos(self, deco_macs) -> dict:
}
response_json = await self._async_post(
context,
f"http://{self._host}/cgi-bin/luci/;stok={self._stok}/admin/device",
f"{self._host}/cgi-bin/luci/;stok={self._stok}/admin/device",
params={"form": "system"},
data=self._encode_payload(client_payload),
)
Expand All @@ -227,7 +227,7 @@ async def _async_list_clients(self, deco_mac) -> dict:
client_payload = {"operation": "read", "params": {"device_mac": deco_mac}}
response_json = await self._async_post(
context,
f"http://{self._host}/cgi-bin/luci/;stok={self._stok}/admin/client",
f"{self._host}/cgi-bin/luci/;stok={self._stok}/admin/client",
params={"form": "client_list"},
data=self._encode_payload(client_payload),
)
Expand Down Expand Up @@ -263,7 +263,7 @@ async def _async_fetch_keys(self):
context = "Fetch keys"
response_json = await self._async_post(
context,
f"http://{self._host}/cgi-bin/luci/;stok=/login",
f"{self._host}/cgi-bin/luci/;stok=/login",
params={"form": "keys"},
data=json.dumps({"operation": "read"}),
)
Expand All @@ -288,7 +288,7 @@ async def _async_fetch_auth(self):
context = "Fetch auth"
response_json = await self._async_post(
context,
f"http://{self._host}/cgi-bin/luci/;stok=/login",
f"{self._host}/cgi-bin/luci/;stok=/login",
params={"form": "auth"},
data=json.dumps({"operation": "read"}),
)
Expand Down Expand Up @@ -356,7 +356,7 @@ async def _async_login(self):
try:
response_json = await self._async_post(
context,
f"http://{self._host}/cgi-bin/luci/;stok=/login",
f"{self._host}/cgi-bin/luci/;stok=/login",
params={"form": "login"},
data=self._encode_payload(login_payload),
)
Expand Down
15 changes: 11 additions & 4 deletions custom_components/tplink_deco/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from homeassistant.core import callback
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.exceptions import ConfigEntryNotReady

from .__init__ import async_create_and_refresh_coordinators
from .const import CONF_CLIENT_POSTFIX
Expand Down Expand Up @@ -54,7 +55,9 @@ def _get_schema(data: dict[str:Any]):
scan_interval = data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
schema.update(
{
vol.Required(CONF_HOST, default=data.get(CONF_HOST, "10.0.0.1")): str,
vol.Required(
CONF_HOST, default=data.get(CONF_HOST, "http://192.168.0.1")
): str,
vol.Required(
CONF_SCAN_INTERVAL,
default=scan_interval,
Expand Down Expand Up @@ -126,17 +129,21 @@ async def _async_test_credentials(hass: HomeAssistant, data: dict[str:Any]):
except TimeoutException:
return {"base": "timeout_connect"}
except ConfigEntryAuthFailed as err:
_LOGGER.warning("Error authenticating credentials: %s", err)
_LOGGER.error("Error authenticating credentials: %s", err)
return {"base": "invalid_auth"}
except ConfigEntryNotReady as err:
_LOGGER.error("Error connection to host: %s", err)
return {"base": "invalid_host"}
except Exception as err:
_LOGGER.warning("Error testing credentials: %s", err)
_LOGGER.error("Error testing credentials: %s", err)
raise err
return {"base": "unknown"}


class TplinkDecoFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for tplink_deco."""

VERSION = 5
VERSION = 6
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
reauth_entry: ConfigEntry = None

Expand Down
2 changes: 2 additions & 0 deletions custom_components/tplink_deco/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"error": {
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"invalid_host": "[%key:common::config_flow::error::invalid_host%]",
"timeout_connect": "[%key:common::config_flow::error::timeout_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
}
Expand Down Expand Up @@ -57,6 +58,7 @@
},
"error": {
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"invalid_host": "[%key:common::config_flow::error::invalid_host%]",
"timeout_connect": "[%key:common::config_flow::error::timeout_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
}
Expand Down
2 changes: 2 additions & 0 deletions custom_components/tplink_deco/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"error": {
"invalid_auth": "Invalid authentication",
"invalid_host": "Unable to connect to host.",
"timeout_connect": "Timeout establishing connection",
"unknown": "Unexpected error"
}
Expand All @@ -57,6 +58,7 @@
},
"error": {
"invalid_auth": "Invalid authentication",
"invalid_host": "Unable to connect to host.",
"timeout_connect": "Timeout establishing connection",
"unknown": "Unexpected error"
}
Expand Down

0 comments on commit 270bea0

Please sign in to comment.