Пример #1
0
 async def gen(serial, sender, **kwargs):
     assert serial in (light1.serial, light2.serial)
     yield Pipeline([DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")])
Пример #2
0
        for device, msgs in expected.items():
            assert device in devices
            devices.store(device).assertIncoming(*msgs, ignore=[DiscoveryMessages.GetService])
            devices.store(device).clear()

    async it "is able to do a FromGenerator per serial", sender:

        async def gen(serial, sender, **kwargs):
            assert serial in (light1.serial, light2.serial)
            yield Pipeline([DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")])

        msg = FromGeneratorPerSerial(gen)

        expected = {
            light1: [DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")],
            light2: [DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")],
            light3: [],
        }

        errors = []

        got = defaultdict(list)
        try:
            async with light3.offline():
                async for pkt in sender(msg, devices.serials, error_catcher=errors):
                    got[pkt.serial].append(pkt)
        finally:
            assert errors == [FailedToFindDevice(serial=light3.serial)]

        assert len(devices) > 0
Пример #3
0
 async def gen(serial, sender, **kwargs):
     yield Pipeline([DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")])
Пример #4
0
        await server.assertCommand(
            "/v1/lifx/command",
            {
                "command": "set",
                "args": {"pkt_type": 24, "pkt_args": {"label": "blah"}, "matcher": "label=kitchen"},
            },
            json_output=expected,
        )

        assert (
            devices.store(kitchen_light).count(
                Events.INCOMING(
                    kitchen_light,
                    kitchen_light.io["MEMORY"],
                    pkt=DeviceMessages.SetLabel(label="blah"),
                )
            )
            == 1
        )

        for device in devices:
            if device is not kitchen_light:
                devices.store(device).assertNoSetMessages()

    async it "has power_toggle command", devices, server:
        expected = {"results": {device.serial: "ok" for device in devices}}

        for device in devices:
            if device.serial == "d073d5000001":
                await device.change_one("power", 0, event=None)
Пример #5
0
            d.finish = pytest.helpers.AsyncMock(name="finish")

        with assertRaises(ValueError, "NOPE"):
            async with WithDevices(devices):
                for d in devices:
                    d.start.assert_called_once_with()
                    assert len(d.finish.mock_calls) == 0
                raise ValueError("NOPE")

        for d in devices:
            d.finish.assert_called_once_with()

describe "pktkeys":
    it "can get us deduped keys to represent the packets":
        msg1 = DeviceMessages.SetPower(level=65535, source=1, sequence=2, target="d073d501")
        msg2 = DeviceMessages.SetLabel(label="bob", source=3, sequence=4, target="d073d502")
        msg3 = DeviceMessages.SetLabel(label="bob", source=5, sequence=6, target="d073d503")
        keys = pktkeys([msg1, msg2, msg3])

        assert keys == [(1024, 21, '{"level": 65535}'), (1024, 24, '{"label": "bob"}')]

    it "can be told to keep duplicates":
        msg1 = DeviceMessages.SetPower(level=65535, source=1, sequence=2, target="d073d501")
        msg2 = DeviceMessages.SetLabel(label="bob", source=3, sequence=4, target="d073d502")
        msg3 = DeviceMessages.SetLabel(label="bob", source=5, sequence=6, target="d073d503")
        keys = pktkeys([msg1, msg2, msg3], keep_duplicates=True)

        assert keys == [
            (1024, 21, '{"level": 65535}'),
            (1024, 24, '{"label": "bob"}'),
            (1024, 24, '{"label": "bob"}'),
Пример #6
0
        assert sorted(called) == sorted(devices.serials)

    async it "can wait between messages", sender:
        got_times = defaultdict(list)

        async def see_request(event):
            got_times[event.pkt.serial].append(time.time())

        isr1 = light1.io["MEMORY"].packet_filter.intercept_see_request(see_request)
        isr2 = light2.io["MEMORY"].packet_filter.intercept_see_request(see_request)
        isr3 = light3.io["MEMORY"].packet_filter.intercept_see_request(see_request)

        msg = Pipeline(
            DeviceMessages.SetPower(level=0),
            DeviceMessages.SetLabel(label="wat"),
            LightMessages.SetColor(hue=0, saturation=0, brightness=1, kelvin=4500),
            spread=3,
        )

        got = defaultdict(list)

        with isr1, isr2, isr3:
            start = time.time()
            async for pkt in sender(msg, devices.serials):
                got[pkt.serial].append(pkt)
            assert time.time() - start == 9

        assert all(serial in got for serial in devices.serials), got
        assert all(len(got[serial]) == 3 for serial in devices.serials), got
Пример #7
0
        for device in runner.devices:
            if device not in expected:
                assert False, f"No expectation for {device.serial}"

            device.compare_received(expected[device])

    async it "is able to do a FromGenerator per serial", runner:

        async def gen(serial, sender, **kwargs):
            assert serial in (light1.serial, light2.serial)
            yield Pipeline([DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")])

        msg = FromGeneratorPerSerial(gen)

        expected = {
            light1: [DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")],
            light2: [DeviceMessages.GetPower(), DeviceMessages.SetLabel(label="wat")],
            light3: [],
        }

        errors = []

        got = defaultdict(list)
        with light3.offline():
            async for pkt in runner.sender(msg, runner.serials, error_catcher=errors):
                got[pkt.serial].append(pkt)

        assert len(runner.devices) > 0

        for device in runner.devices:
            if device not in expected:
Пример #8
0
    describe "LightStateResponder":

        @pytest.fixture()
        async def device(self):
            device = Device(chp.LightStateResponder())
            async with device:
                device.assertAttrs(label="", power=0, color=chp.Color(0, 0, 1, 3500))
                yield device

        async it "responds to label messages", device:
            await device.assertResponse(
                DeviceMessages.GetLabel(), [DeviceMessages.StateLabel(label="")]
            )
            await device.assertResponse(
                DeviceMessages.SetLabel(label="sam"),
                [DeviceMessages.StateLabel(label="sam")],
                label="sam",
            )
            await device.assertResponse(
                DeviceMessages.GetLabel(), [DeviceMessages.StateLabel(label="sam")], label="sam"
            )

        async it "responds to power messages", device:
            await device.assertResponse(
                DeviceMessages.GetPower(), [DeviceMessages.StatePower(level=0)]
            )
            await device.assertResponse(
                DeviceMessages.SetPower(level=200), [DeviceMessages.StatePower(level=0)], power=200
            )
            await device.assertResponse(