Skip to content

Commit

Permalink
power: fix 'address' option percent encoding
Browse files Browse the repository at this point in the history
Allow "/" separators in the address.  This patch also resolves a regression in  the generic http client.

Signed-off-by:  Eric Callahan <[email protected]>
  • Loading branch information
Arksine committed Aug 11, 2024
1 parent e66744b commit f735c04
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions moonraker/components/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,14 @@ def __init__(
) -> None:
super().__init__(config)
self.client: HttpClient = self.server.lookup_component("http_client")
if is_generic:
return
self.addr: str = config.get("address")
self.port = config.getint("port", default_port)
self.user = config.load_template("user", default_user).render()
self.password = config.load_template("password", default_password).render()
self.has_basic_auth: bool = False
if is_generic:
return
addr_parts = config.get("address").strip("/").split("/")
self.addr: str = "/".join([quote(part) for part in addr_parts])
self.port = config.getint("port", default_port)
self.protocol = config.get("protocol", default_protocol)
if self.port == -1:
self.port = 443 if self.protocol.lower() == "https" else 80
Expand Down Expand Up @@ -990,7 +991,7 @@ async def _send_tasmota_command(self, command: str) -> Dict[str, Any]:
"password": self.password,
"cmnd": out_cmd
})
url = f"{self.protocol}://{quote(self.addr)}/cm?{query}"
url = f"{self.protocol}://{self.addr}/cm?{query}"
return await self._send_http_command(url, command)

async def _send_status_request(self) -> str:
Expand Down Expand Up @@ -1035,7 +1036,7 @@ async def _send_shelly_command(self, command: str) -> Dict[str, Any]:
elif command != "info":
raise self.server.error(f"Invalid shelly command: {command}")
query = urlencode(query_args)
url = f"{self.protocol}://{quote(self.addr)}/{out_cmd}?{query}"
url = f"{self.protocol}://{self.addr}/{out_cmd}?{query}"
return await self._send_http_command(url, command)

async def _send_status_request(self) -> str:
Expand All @@ -1062,7 +1063,7 @@ async def _send_smartthings_command(self, command: str) -> Dict[str, Any]:
if (command == "on" or command == "off"):
method = "POST"
url = (
f"{self.protocol}://{quote(self.addr)}"
f"{self.protocol}://{self.addr}"
f"/v1/devices/{quote(self.device)}/commands"
)
body = [
Expand All @@ -1075,7 +1076,7 @@ async def _send_smartthings_command(self, command: str) -> Dict[str, Any]:
elif command == "info":
method = "GET"
url = (
f"{self.protocol}://{quote(self.addr)}/v1/devices/"
f"{self.protocol}://{self.addr}/v1/devices/"
f"{quote(self.device)}/components/main/capabilities/"
"switch/status"
)
Expand Down Expand Up @@ -1124,7 +1125,7 @@ async def _send_homeseer(
query_args["label"] = state
query = urlencode(query_args)
url = (
f"{self.protocol}://{quote(self.addr)}:{self.port}/JSON?{query}"
f"{self.protocol}://{self.addr}:{self.port}/JSON?{query}"
)
return await self._send_http_command(url, request)

Expand Down Expand Up @@ -1159,7 +1160,7 @@ async def _send_homeassistant_command(self, command: str) -> Dict[str, Any]:
else:
raise self.server.error(
f"Invalid homeassistant command: {command}")
url = f"{self.protocol}://{quote(self.addr)}:{self.port}/{out_cmd}"
url = f"{self.protocol}://{self.addr}:{self.port}/{out_cmd}"
headers = {
'Authorization': f'Bearer {self.token}'
}
Expand Down Expand Up @@ -1198,7 +1199,7 @@ async def _send_loxonev1_command(self, command: str) -> Dict[str, Any]:
out_cmd = f"jdev/sps/io/{quote(self.output_id)}"
else:
raise self.server.error(f"Invalid loxonev1 command: {command}")
url = f"http://{quote(self.addr)}/{out_cmd}"
url = f"http://{self.addr}/{out_cmd}"
return await self._send_http_command(url, command)

async def _send_status_request(self) -> str:
Expand Down Expand Up @@ -1408,7 +1409,7 @@ def __init__(self, config: ConfigHelper) -> None:
async def _send_power_request(self, state: str) -> str:
new_state = True if state == "on" else False
url = (
f"{self.protocol}://{quote(self.addr)}:{self.port}/api/{quote(self.user)}"
f"{self.protocol}://{self.addr}:{self.port}/api/{quote(self.user)}"
f"/{self.device_type}s/{quote(self.device_id)}"
f"/{quote(self.state_key)}"
)
Expand All @@ -1424,7 +1425,7 @@ async def _send_power_request(self, state: str) -> str:

async def _send_status_request(self) -> str:
url = (
f"{self.protocol}://{quote(self.addr)}:{self.port}/api/{quote(self.user)}"
f"{self.protocol}://{self.addr}:{self.port}/api/{quote(self.user)}"
f"/{self.device_type}s/{quote(self.device_id)}"
)
ret = await self.client.request("GET", url)
Expand Down

0 comments on commit f735c04

Please sign in to comment.