Exemplo n.º 1
0
 def test_stop_server(self, MockServer) -> None:
     """Hub should stop a server when stopped."""
     hub = Hub("1.2.3.4", "1234", "token", True)
     run(hub.start())
     self.assertTrue(MockServer.return_value.start.called)
     hub.stop()
     self.assertTrue(MockServer.return_value.stop.called)
Exemplo n.º 2
0
 def test_custom_event_url_without_port(self, MockServer) -> None:
     """Event URL should use custom port if none was provided."""
     MockServer.return_value.url = "http://127.0.0.1:81"
     hub = Hub("1.2.3.4", "1234", "token", 420, event_url="http://foo.local")
     wait_for(hub.start())
     url = unquote(requests[0]["url"])
     self.assertRegex(url, r"http://foo\.local:420$")
Exemplo n.º 3
0
async def test_stop_server(MockServer) -> None:
    """Hub should stop a server when stopped."""
    hub = Hub("1.2.3.4", "1234", "token", True)
    await hub.start()
    assert MockServer.return_value.start.called is True
    hub.stop()
    assert MockServer.return_value.stop.called is True
Exemplo n.º 4
0
 def test_custom_event_url(self, MockServer) -> None:
     """Event URL should be configurable."""
     MockServer.return_value.url = "http://127.0.0.1:81"
     hub = Hub("1.2.3.4", "1234", "token", event_url="http://foo.local")
     wait_for(hub.start())
     url = unquote(requests[0]["url"])
     self.assertRegex(url, r"http://foo\.local$")
Exemplo n.º 5
0
 def test_default_event_url(self, MockServer) -> None:
     """Default event URL should be server URL."""
     MockServer.return_value.url = "http://127.0.0.1:81"
     hub = Hub("1.2.3.4", "1234", "token")
     wait_for(hub.start())
     url = unquote(requests[0]["url"])
     self.assertRegex(url, r"http://127.0.0.1:81$")
Exemplo n.º 6
0
 def test_start(self, MockServer) -> None:
     """start() should request data from the Hubitat hub."""
     hub = Hub("1.2.3.4", "1234", "token")
     run(hub.start())
     # 33 requests - 1 to get device list, 32 to update devices
     self.assertEqual(len(requests), 33)
     self.assertRegex(requests[1]["url"], "devices$")
     self.assertRegex(requests[2]["url"], "devices/\d+$")
     self.assertRegex(requests[-1]["url"], "devices/\d+$")
Exemplo n.º 7
0
 def test_start_no_hsm(self, MockServer) -> None:
     hub = Hub("1.2.3.4", "1234", "token")
     wait_for(hub.start())
     self.assertEqual(len(requests), 13)
     self.assertRegex(requests[1]["url"], "devices$")
     self.assertRegex(requests[2]["url"], r"devices/\d+$")
     self.assertRegex(requests[3]["url"], r"devices/\d+$")
     self.assertRegex(requests[-2]["url"], "modes$")
     self.assertRegex(requests[-1]["url"], "hsm$")
Exemplo n.º 8
0
async def test_process_set_hsm() -> None:
    """Started hub should allow mode to be updated."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    assert hub.hsm_status == "armedAway"
    await hub.set_hsm(HSM_DISARM)
    assert re.search(f"hsm/{HSM_DISARM}$", requests[-1]["url"]) is not None

    hub._process_event(events["hsmAllDisarmed"])
    assert hub.hsm_status == "allDisarmed"
Exemplo n.º 9
0
async def test_process_set_mode() -> None:
    """Started hub should allow mode to be updated."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    assert hub.mode == "Day"
    await hub.set_mode("Evening")
    assert re.search("modes/2$", requests[-1]["url"]) is not None

    hub._process_event(events["mode"])
    assert hub.mode == "Evening"
Exemplo n.º 10
0
async def test_process_event() -> None:
    """Started hub should process a device event."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    device = hub.devices["176"]
    attr = device.attributes["switch"]
    assert attr.value == "off"

    hub._process_event(events["device"])

    attr = device.attributes["switch"]
    assert attr.value == "on"
Exemplo n.º 11
0
    def test_process_event(self, MockServer) -> None:
        """Started hub should process a device event."""
        hub = Hub("1.2.3.4", "1234", "token")
        run(hub.start())
        device = hub.devices["176"]
        attr = device.attributes["switch"]
        self.assertEqual(attr.value, "off")

        hub.process_event(events[0])

        attr = device.attributes["switch"]
        self.assertEqual(attr.value, "on")
Exemplo n.º 12
0
    def test_process_other_event(self, MockServer) -> None:
        """Started hub should ignore non-device, non-mode events."""
        hub = Hub("1.2.3.4", "1234", "token")
        wait_for(hub.start())
        device = hub.devices["176"]
        attr = device.attributes["switch"]
        self.assertEqual(attr.value, "off")

        hub._process_event(events["other"])

        attr = device.attributes["switch"]
        self.assertEqual(attr.value, "off")
Exemplo n.º 13
0
async def test_process_other_event() -> None:
    """Started hub should ignore non-device, non-mode events."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    device = hub.devices["176"]
    attr = device.attributes["switch"]
    assert attr.value == "off"

    hub._process_event(events["other"])

    attr = device.attributes["switch"]
    assert attr.value == "off"
Exemplo n.º 14
0
    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}$")
Exemplo n.º 15
0
async def test_default_event_url(MockServer) -> None:
    """Default event URL should be server URL."""
    MockServer.return_value.url = "http://127.0.0.1:81"
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    url = unquote(requests[0]["url"])
    assert re.search(r"http://127.0.0.1:81$", url) is not None
Exemplo n.º 16
0
async def test_custom_event_url_without_port(MockServer) -> None:
    """Event URL should use custom port if none was provided."""
    MockServer.return_value.url = "http://127.0.0.1:81"
    hub = Hub("1.2.3.4", "1234", "token", 420, event_url="http://foo.local")
    await hub.start()
    url = unquote(requests[0]["url"])
    assert re.search(r"http://foo\.local:420$", url) is not None
Exemplo n.º 17
0
async def test_custom_event_url(MockServer) -> None:
    """Event URL should be configurable."""
    MockServer.return_value.url = "http://127.0.0.1:81"
    hub = Hub("1.2.3.4", "1234", "token", event_url="http://foo.local")
    await hub.start()
    url = unquote(requests[0]["url"])
    assert re.search(r"http://foo\.local$", url) is not None
Exemplo n.º 18
0
 def test_start(self, MockServer) -> None:
     """start() should request data from the Hubitat hub."""
     hub = Hub("1.2.3.4", "1234", "token")
     wait_for(hub.start())
     # 13 requests:
     #   0: set event URL
     #   1: request devices
     #   2...: request device details
     #   -2: request modes
     #   -1: request hsm status
     self.assertEqual(len(requests), 13)
     self.assertRegex(requests[1]["url"], "devices$")
     self.assertRegex(requests[2]["url"], r"devices/\d+$")
     self.assertRegex(requests[3]["url"], r"devices/\d+$")
     self.assertRegex(requests[-2]["url"], "modes$")
     self.assertRegex(requests[-1]["url"], "hsm$")
Exemplo n.º 19
0
async def test_set_port(MockServer) -> None:
    """Started hub should allow port to be set."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    assert MockServer.call_args[0][2] == 0
    await hub.set_port(14)
    assert MockServer.call_args[0][2] == 14
Exemplo n.º 20
0
    def test_process_hsm_event(self, MockServer) -> None:
        """Started hub should emit HSM events."""
        hub = Hub("1.2.3.4", "1234", "token")
        wait_for(hub.start())

        handler_called = False

        def listener(_: Any):
            nonlocal handler_called
            handler_called = True

        hub._process_event(events["hsm"])
        self.assertFalse(handler_called)

        hub.add_hsm_listener(listener)
        hub._process_event(events["hsm"])
        self.assertTrue(handler_called)
Exemplo n.º 21
0
async def test_set_event_url(MockServer) -> None:
    """Started hub should allow event URL to be set."""
    server_url = "http://127.0.0.1:81"
    MockServer.return_value.url = server_url
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()

    await hub.set_event_url(None)
    event_url = unquote(requests[-1]["url"])
    assert re.search(f"postURL/{server_url}$", event_url) is not None

    other_url = "http://10.0.1.1:4443"
    await hub.set_event_url(other_url)
    event_url = unquote(requests[-1]["url"])
    assert re.search(f"postURL/{other_url}$", event_url) is not None
Exemplo n.º 22
0
async def test_process_hsm_event() -> None:
    """Started hub should emit HSM events."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()

    handler_called = False

    def listener(_: Any):
        nonlocal handler_called
        handler_called = True

    hub._process_event(events["hsmArmedAway"])
    assert handler_called is False

    hub.add_hsm_listener(listener)
    hub._process_event(events["hsmArmedAway"])
    assert handler_called is True
Exemplo n.º 23
0
    def test_process_set_hsm(self, MockServer) -> None:
        """Started hub should allow mode to be updated."""
        hub = Hub("1.2.3.4", "1234", "token")
        wait_for(hub.start())
        self.assertEqual(hub.hsm_status, "armedAway")
        wait_for(hub.set_hsm(HSM_DISARM))
        self.assertRegex(requests[-1]["url"], f"hsm/{HSM_DISARM}$")

        hub._process_event(events["hsm"])
        self.assertEqual(hub.hsm_status, "armedAway")
Exemplo n.º 24
0
    def test_process_set_mode(self, MockServer) -> None:
        """Started hub should allow mode to be updated."""
        hub = Hub("1.2.3.4", "1234", "token")
        wait_for(hub.start())
        self.assertEqual(hub.mode, "Day")
        wait_for(hub.set_mode("Evening"))
        self.assertRegex(requests[-1]["url"], "modes/2$")

        hub._process_event(events["mode"])
        self.assertEqual(hub.mode, "Evening")
Exemplo n.º 25
0
async def test_start() -> None:
    """start() should request data from the Hubitat hub."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    # 13 requests:
    #   0: set event URL
    #   1: request devices
    #   2...: request device details
    #   -2: request modes
    #   -1: request hsm status
    assert len(requests) == 13
    assert re.search("devices$", requests[1]["url"]) is not None
    assert re.search(r"devices/\d+$", requests[2]["url"]) is not None
    assert re.search(r"devices/\d+$", requests[3]["url"]) is not None
    assert re.search("modes$", requests[-2]["url"]) is not None
    assert re.search("hsm$", requests[-1]["url"]) is not None
Exemplo n.º 26
0
async def test_mode_is_supported() -> None:
    """hub should indicate if HSM is supported."""
    hub = Hub("1.2.3.4", "1234", "token")
    assert hub.mode_supported is None
    await hub.start()
    assert hub.mode_supported is True
Exemplo n.º 27
0
async def test_devices_loaded() -> None:
    """Started hub should have parsed device info."""
    hub = Hub("1.2.3.4", "1234", "token")
    await hub.start()
    assert len(hub.devices) == 9
Exemplo n.º 28
0
async def test_custom_event_port(MockServer) -> None:
    """Event server port should be configurable."""
    MockServer.return_value.url = "http://127.0.0.1:81"
    hub = Hub("1.2.3.4", "1234", "token", 420)
    await hub.start()
    assert MockServer.call_args[0][2] == 420
Exemplo n.º 29
0
async def test_custom_event_port_and_url(MockServer) -> None:
    """Explicit event server port should override port from URL."""
    MockServer.return_value.url = "http://127.0.0.1:81"
    hub = Hub("1.2.3.4", "1234", "token", 420, "http://foo.local:416")
    await hub.start()
    assert MockServer.call_args[0][2] == 420
Exemplo n.º 30
0
async def test_custom_event_port_from_url(MockServer) -> None:
    """Event server port should come from event URL if none was provided."""
    MockServer.return_value.url = "http://127.0.0.1:81"
    hub = Hub("1.2.3.4", "1234", "token", event_url="http://foo.local:416")
    await hub.start()
    assert MockServer.call_args[0][2] == 416