Skip to content

Commit

Permalink
User server URL as default event URL
Browse files Browse the repository at this point in the history
  • Loading branch information
jason0x43 committed Oct 26, 2020
1 parent 5d6ab98 commit fda0605
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions hubitatmaker/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ async def send_command(
_LOGGER.debug("Sending command %s(%s) to %s", command, arg, device_id)
return await self._api_request(path)

async def set_event_url(self, event_url: str) -> None:
async def set_event_url(self, event_url: Optional[str]) -> None:
"""Set the URL that Hubitat will POST device events to."""
_LOGGER.info("Setting event update URL to %s", event_url)
if not event_url:
event_url = self._server.url
url = quote(str(event_url), safe="")
_LOGGER.info("Setting event update URL to %s", url)
await self._api_request(f"postURL/{url}")

async def set_hsm(self, hsm_mode: str) -> None:
Expand Down Expand Up @@ -378,7 +380,7 @@ async def _start_server(self) -> None:
self._server.start()
_LOGGER.debug("Listening on %s:%d", address, self._server.port)

await self.set_event_url(self.event_url or self._server.url)
await self.set_event_url(self.event_url)


@contextmanager
Expand Down
19 changes: 19 additions & 0 deletions hubitatmaker/tests/test_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,22 @@ def test_process_set_mode(self, MockServer) -> None:

hub._process_event(events["mode"])
self.assertEqual(hub.mode, "Evening")

@patch("aiohttp.request", new=fake_request)
@patch("getmac.get_mac_address", new=fake_get_mac_address)
@patch("hubitatmaker.server.Server")
def test_set_event_url(self, MockServer) -> None:
"""Started hub should allow mode to be updated."""
server_url = "http://127.0.0.1:81"
MockServer.return_value.url = server_url
hub = Hub("1.2.3.4", "1234", "token")
wait_for(hub.start())

wait_for(hub.set_event_url(None))
event_url = unquote(requests[-1]["url"])
self.assertRegex(event_url, f"postURL/{server_url}$")

other_url = "http://10.0.1.1:4443"
wait_for(hub.set_event_url(other_url))
event_url = unquote(requests[-1]["url"])
self.assertRegex(event_url, f"postURL/{other_url}$")

0 comments on commit fda0605

Please sign in to comment.