async def respond(s, event): if event | DeviceMessages.GetPower: event.set_replies(DeviceMessages.StatePower(level=s.device_attrs.power)) elif event | DeviceMessages.SetPower: event.set_replies(DeviceMessages.StatePower(level=s.device_attrs.power)) await s.device_attrs.attrs_apply( s.device_attrs.attrs_path("power").changer_to(event.pkt.level), event=event )
async def respond(s, event): if event | DeviceMessages.GetPower: event.set_replies(DeviceMessages.StatePower(level=s.device_attrs.power)) if event | DeviceMessages.GetLabel: event.ignore_request() elif event | DeviceMessages.SetPower: event.set_replies(DeviceMessages.StatePower(level=s.device_attrs.power)) await s.device_attrs.attrs_apply( s.device_attrs.attrs_path("power").changer_to(event.pkt.level), event=None, ) elif event | DiscoveryMessages.GetService: event.set_replies( DiscoveryMessages.StateService(service=Services.UDP, port=56700), DiscoveryMessages.StateService(service=Services.UDP, port=76500), )
async def __anext__(self): self.index += 1 if self.index == 0: return ( DeviceMessages.StatePower(level=0, target="d073d5000001"), ("192.168.0.1", 56700), "192.168.0.1", ) elif self.index == 1: self.kwargs["error_catcher"](PhotonsAppError("failure", serial="d073d5000002")) raise StopAsyncIteration
async def __anext__(self): self.index += 1 if self.index == 0: return ( DeviceMessages.StatePower(level=0, target="d073d5000001"), ("192.168.0.1", 56700), "192.168.0.1", ) elif self.index == 1: return ( DeviceMessages.StateHostFirmware( build=0, version_major=1, version_minor=2, target="d073d5000002" ), ("`92.168.0.2", 56700), "192.168.0.2", ) else: raise StopAsyncIteration
with assertRaises(Exception): pkt.pack() e = EKLS(device, io, pkt=pkt) assert e.bts is None pkt.source = 2 pkt.sequence = 1 pkt.target = None assert e.bts == pkt.pack() it "can set replies", EKLS, device, io: e = EKLS(device, io, pkt=DeviceMessages.GetPower()) assert not e.handled assert e.replies is None replies = [DeviceMessages.StatePower(level=0), DeviceMessages.StatePower(level=65535)] e.set_replies(*replies) assert e.handled assert e.replies == replies other = [DeviceMessages.StateLabel()] e.set_replies(*other) assert e.handled assert e.replies == other it "can add replies", EKLS, device, io: reply1 = DeviceMessages.StatePower() reply2 = DeviceMessages.StateLabel() reply3 = DeviceMessages.StateGroup() reply4 = DeviceMessages.StateLocation()
plan_kwargs.update(thing) plans = sender.make_plans(*plan_args, **plan_kwargs) return dict(await sender.gatherer.gather_all(plans, reference, **kwargs)) describe "PacketPlan": async it "gets the packet", sender: plan = PacketPlan(DeviceMessages.GetPower(), DeviceMessages.StatePower) got = await self.gather(sender, two_lights, {"result": plan}) assert got == { light1.serial: (True, {"result": mock.ANY}), light2.serial: (True, {"result": mock.ANY}), } pytest.helpers.print_packet_difference( got[light1.serial][1]["result"], DeviceMessages.StatePower(level=0) ) pytest.helpers.print_packet_difference( got[light2.serial][1]["result"], DeviceMessages.StatePower(level=65535) ) async it "fails if we can't get the correct response", sender: plan = PacketPlan(DeviceMessages.GetPower(), DeviceMessages.StateLabel) got = await self.gather(sender, two_lights, {"result": plan}) assert got == {} describe "PresencePlan": async it "returns True", sender: got = await self.gather(sender, two_lights, "presence") assert got == {
@pytest.fixture() def assertResponse(self, device, **attrs): return makeAssertResponse(device, **attrs) @pytest.fixture() def assertState(self, device, **attrs): return makeAssertState(device, **attrs) @pytest.fixture() def assertEvent(self, device, **attrs): return makeAssertEvent(device, **attrs) async it "can change the power of all the relays", device, assertResponse, assertEvent: await assertResponse( DeviceMessages.GetPower(), [DeviceMessages.StatePower(level=0)], ) await assertResponse( DeviceMessages.SetPower(level=65535), [DeviceMessages.StatePower(level=0)], relays=[ Relay.create(power=65535), Relay.create(power=65535), Relay.create(power=65535), Relay.create(power=65535), ], power=65535, ) await assertResponse(
assert len(choose_transport.mock_calls) == 0 V.transport.spawn.assert_called_once_with(V.original, timeout=V.connect_timeout) describe "received_data": async it "unpacks bytes and sends to the receiver", V: allow_zero = mock.Mock(name="allow_zero") addr = mock.Mock(name="addr") def recv(pkt, addr, *, allow_zero): assert pkt | DeviceMessages.StatePower assert pkt.level == 100 recv = pytest.helpers.AsyncMock(name="recv", side_effect=recv) with mock.patch.object(V.communication.receiver, "recv", recv): pkt = DeviceMessages.StatePower(level=100, source=1, sequence=1, target=None) data = pkt.pack().tobytes() await V.communication.received_data(data, addr, allow_zero=allow_zero) recv.assert_called_once_with(mock.ANY, addr, allow_zero=allow_zero) async it "unpacks unknown packets", V: allow_zero = mock.Mock(name="allow_zero") addr = mock.Mock(name="addr") def recv(pkt, addr, *, allow_zero): assert isinstance(pkt, LIFXPacket) assert pkt.pkt_type == 9001 assert pkt.payload == b"things" recv = pytest.helpers.AsyncMock(name="recv", side_effect=recv)
} assert dct == { "results": {"one": {"pkt_type": 1}, "two": "ok", "three": {"error": "blah"}} } it "includes errors on result": builder = ihp.ResultBuilder(["one", "two"]) builder.result["errors"] = ["error1", "error2"] dct = builder.as_dict() assert builder.result == {"results": {}, "errors": ["error1", "error2"]} assert dct == {"results": {"one": "ok", "two": "ok"}, "errors": ["error1", "error2"]} describe "add_packet": it "sets info for that serial in results": packet = DeviceMessages.StatePower(level=0, target="d073d5000001") info = {"pkt_type": 22, "pkt_name": "StatePower", "payload": {"level": 0}} builder = ihp.ResultBuilder(["d073d5000001"]) builder.add_packet(packet) assert builder.as_dict() == {"results": {"d073d5000001": info}} it "makes a list if already have packet for that bulb": packet1 = DeviceMessages.StatePower(level=0, target="d073d5000001") packet2 = DeviceMessages.StatePower(level=65535, target="d073d5000001") packet3 = DeviceMessages.StateHostFirmware( build=0, version_major=1, version_minor=2, target="d073d5000001" ) info1 = {"pkt_type": 22, "pkt_name": "StatePower", "payload": {"level": 0}} info2 = {"pkt_type": 22, "pkt_name": "StatePower", "payload": {"level": 65535}}
assert reply.serial == device.serial describe "SendReplies": it "takes in an event", incoming_event: assert SendReplies(incoming_event).event is incoming_event async it "fills out each reply on the event with source, sequence, target if missing", incoming_event, device: sr = SendReplies(incoming_event) replies = [] async for msg in sr.process(): replies.append(msg) assert replies == [] sending = [ DeviceMessages.StatePower(), DeviceMessages.StatePower(source=45), DeviceMessages.StatePower(sequence=76), DeviceMessages.StatePower(sequence=78, target="d073d5999999"), DeviceMessages.StatePower(target=None), ] incoming_event.add_replies(*sending) for msg in sending: assert msg.source != 20 assert msg.sequence != 34 assert msg.serial != device.serial replies = [] async for msg in sr.process(): replies.append(msg)
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( DeviceMessages.GetPower(), [DeviceMessages.StatePower(level=200)], power=200 ) async it "responds to light power messages", device: await device.assertResponse( DeviceMessages.GetPower(), [DeviceMessages.StatePower(level=0)] ) await device.assertResponse( LightMessages.SetLightPower(level=200), [LightMessages.StateLightPower(level=0)],
def assertResponse(self, device, **attrs): return makeAssertResponse(device, **attrs) async it "responds to label messages", device, assertResponse: await assertResponse(DeviceMessages.GetLabel(), [DeviceMessages.StateLabel(label="")]) await assertResponse( DeviceMessages.SetLabel(label="sam"), [DeviceMessages.StateLabel(label="sam")], label="sam", ) await assertResponse( DeviceMessages.GetLabel(), [DeviceMessages.StateLabel(label="sam")], label="sam" ) async it "responds to power messages", device, assertResponse: await assertResponse(DeviceMessages.GetPower(), [DeviceMessages.StatePower(level=0)]) await assertResponse( DeviceMessages.SetPower(level=200), [DeviceMessages.StatePower(level=0)], power=200 ) await assertResponse( DeviceMessages.GetPower(), [DeviceMessages.StatePower(level=200)], power=200 ) describe "SwitchDevice": @pytest.fixture() def device(self): device = devices["switch"] devices.store(device).assertAttrs(label="") return device