def handle_join(self, nwk, ieee, parent_nwk): ieee = t.EUI64(ieee) LOGGER.info("Device 0x%04x (%s) joined the network", nwk, ieee) if ieee in self.devices: dev = self.get_device(ieee) if dev.nwk != nwk: LOGGER.debug("Device %s changed id (0x%04x => 0x%04x)", ieee, dev.nwk, nwk) dev.nwk = nwk dev.schedule_group_membership_scan() elif dev.initializing or dev.status == zigpy.device.Status.ENDPOINTS_INIT: LOGGER.debug("Skip initialization for existing device %s", ieee) dev.schedule_group_membership_scan() return else: dev = self.add_device(ieee, nwk) self.listener_event("device_joined", dev) dev.schedule_initialize()
async def test_startup_ai(app): auto_form = True await _test_startup(app, 0x00, auto_form) assert app._nwk == 0x0000 assert app._ieee == t.EUI64(range(1, 9)) assert app.form_network.call_count == 0 auto_form = False await _test_startup(app, 0x00, auto_form) assert app._nwk == 0x0000 assert app._ieee == t.EUI64(range(1, 9)) assert app.form_network.call_count == 0 auto_form = True await _test_startup(app, 0x06, auto_form) assert app._nwk == 0xFFFE assert app._ieee == t.EUI64(range(1, 9)) assert app.form_network.call_count == 1 auto_form = False await _test_startup(app, 0x06, auto_form) assert app._nwk == 0xFFFE assert app._ieee == t.EUI64(range(1, 9)) assert app.form_network.call_count == 0 auto_form = True await _test_startup(app, 0x00, auto_form, zs=1) assert app._nwk == 0x0000 assert app._ieee == t.EUI64(range(1, 9)) assert app.form_network.call_count == 1 auto_form = False await _test_startup(app, 0x06, auto_form, legacy_module=True) assert app._nwk == 0xFFFE assert app._ieee == t.EUI64(range(1, 9)) assert app.form_network.call_count == 0 auto_form = True await _test_startup(app, 0x00, auto_form, zs=1, legacy_module=True) assert app._nwk == 0x0000 assert app._ieee == t.EUI64(range(1, 9)) assert app.form_network.call_count == 1
async def test_broadcast(): from zigpy.profiles import zha app = mock.MagicMock() app.broadcast.side_effect = asyncio.coroutine(mock.MagicMock()) app.ieee = t.EUI64(map(t.uint8_t, [8, 9, 10, 11, 12, 13, 14, 15])) (profile, cluster, src_ep, dst_ep, data) = ( zha.PROFILE_ID, 1, 2, 3, b"\x02\x01\x00", ) await device.broadcast(app, profile, cluster, src_ep, dst_ep, 0, 0, 123, data) assert app.broadcast.call_count == 1 assert app.broadcast.call_args[0][0] == profile assert app.broadcast.call_args[0][1] == cluster assert app.broadcast.call_args[0][2] == src_ep assert app.broadcast.call_args[0][3] == dst_ep assert app.broadcast.call_args[0][7] == data
async def _test_request( app, expect_reply=True, send_success=True, send_timeout=False, is_end_device=None, **kwargs ): seq = 123 nwk = 0x2345 ieee = t.EUI64(b"\x01\x02\x03\x04\x05\x06\x07\x08") dev = app.add_device(ieee, nwk) dev.node_desc = mock.MagicMock() dev.node_desc.is_end_device = is_end_device def _mock_command( cmdname, ieee, nwk, src_ep, dst_ep, cluster, profile, radius, options, data ): send_fut = asyncio.Future() if not send_timeout: if send_success: send_fut.set_result(xbee_t.TXStatus.SUCCESS) else: send_fut.set_result(xbee_t.TXStatus.ADDRESS_NOT_FOUND) return send_fut app._api._command = mock.MagicMock(side_effect=_mock_command) return await app.request( dev, 0x0260, 1, 2, 3, seq, b"\xaa\x55\xbe\xef", expect_reply=expect_reply, **kwargs )
def handle_join(self, nwk: t.NWK, ieee: t.EUI64, parent_nwk: t.NWK) -> None: """ Called when a device joins or announces itself on the network. """ ieee = t.EUI64(ieee) try: dev = self.get_device(ieee) LOGGER.info("Device 0x%04x (%s) joined the network", nwk, ieee) new_join = False except KeyError: dev = self.add_device(ieee, nwk) LOGGER.info("New device 0x%04x (%s) joined the network", nwk, ieee) new_join = True if dev.nwk != nwk: dev.nwk = nwk LOGGER.debug("Device %s changed id (0x%04x => 0x%04x)", ieee, dev.nwk, nwk) new_join = True # Not all stacks send a ZDO command when a device joins so the last_seen should # be updated dev.update_last_seen() if new_join: self.listener_event("device_joined", dev) dev.schedule_initialize() elif not dev.is_initialized: # Re-initialize partially-initialized devices but don't emit "device_joined" dev.schedule_initialize() else: # Rescan groups for devices that are not newly joining and initialized dev.schedule_group_membership_scan()
async def bind_ieee(app, listener, ieee, cmd, data, service): from zigpy import types as t from zigpy.zdo.types import MultiAddress if ieee is None or not data: LOGGER.error("missing ieee") return LOGGER.debug("running 'bind ieee' command: %s", service) src_dev = app.get_device(ieee=ieee) dst_ieee = t.EUI64([t.uint8_t(p, base=16) for p in data.split(":")]) dst_dev = app.get_device(ieee=dst_ieee) zdo = src_dev.zdo src_clusters = [6, 8, 768] for src_cluster in src_clusters: src_endpoints = [ ep_id for ep_id, ep in src_dev.endpoints.items() if ep_id != 0 and src_cluster in ep.out_clusters ] LOGGER.debug( "0x%04x: got the %s endpoints for %s cluster", src_dev.nwk, src_endpoints, src_cluster, ) if not src_endpoints: LOGGER.debug("0x%04x: skipping %s cluster as non present", src_dev.nwk, src_cluster) continue dst_addr = MultiAddress() dst_addr.addrmode = t.uint8_t(3) dst_addr.ieee = dst_dev.ieee # find dest ep dst_epid = None for ep_id, ep in dst_dev.endpoints.items(): if ep_id == 0: continue if src_cluster in ep.in_clusters: dst_epid = ep_id break if not dst_epid: continue dst_addr.endpoint = t.uint8_t(dst_epid) for src_ep in src_endpoints: LOGGER.debug( "0x%04x: binding %s, ep: %s, cluster: %s to %s dev %s ep", src_dev.nwk, str(src_dev.ieee), src_ep, src_cluster, str(dst_dev.ieee), dst_epid, ) res = await zdo.request(ZDOCmd.Bind_req, src_dev.ieee, src_ep, src_cluster, dst_addr) LOGGER.debug("0x%04x: binding ieee %s: %s", src_dev.nwk, str(dst_dev.ieee), res)
def dev(): app_mock = mock.MagicMock() app_mock.request.side_effect = asyncio.coroutine(mock.MagicMock()) ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) return device.Device(app_mock, ieee, 65535)
def app(): app = mock.MagicMock() app.ieee = t.EUI64(map(t.uint8_t, [8, 9, 10, 11, 12, 13, 14, 15])) app.get_sequence.return_value = 123 app.request.side_effect = asyncio.coroutine(mock.MagicMock()) return app
def dev(): app_mock = mock.MagicMock() ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) return device.Device(app_mock, ieee, 65535)
def dev(monkeypatch, app): monkeypatch.setattr(device, "APS_REPLY_TIMEOUT_EXTENDED", 0.1) ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) return device.Device(app, ieee, 65535)
def _ieee_to_number( self, ieee ): # a function that will convert a device IEEE to readable numbers ieee_string = str(t.EUI64(map(t.uint8_t, ieee))) return int(ieee_string.replace(':', ''), 16)
def dev(): app_mock = mock.MagicMock(spec_set=ControllerApplication) app_mock.remove.side_effect = asyncio.coroutine(mock.MagicMock()) app_mock.request.side_effect = asyncio.coroutine(mock.MagicMock()) ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) return device.Device(app_mock, ieee, 65535)
def endpoint(app_mock): ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) dev = zigpy.device.Device(app_mock, ieee, 65535) return zigpy.endpoint.Endpoint(dev, 3)
def zdo_f(app): ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) dev = zigpy.device.Device(app, ieee, 65535) dev.request = mock.MagicMock() dev.request.side_effect = asyncio.coroutine(mock.MagicMock()) return zdo.ZDO(dev)
def _ieee_to_number(self, ieee): ieee_string = str(t.EUI64(map(t.uint8_t, ieee))) return int(ieee_string.replace(':', ''), 16)
def endpoint(): app_mock = mock.MagicMock(spec_set=ControllerApplication) ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) dev = zigpy.device.Device(app_mock, ieee, 65535) return zigpy.endpoint.Endpoint(dev, 3)
def dev(monkeypatch): monkeypatch.setattr(device, "APS_REPLY_TIMEOUT_EXTENDED", 0.1) app_mock = mock.MagicMock(spec_set=ControllerApplication) app_mock.remove.side_effect = asyncio.coroutine(mock.MagicMock()) ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) return device.Device(app_mock, ieee, 65535)
def convert_ieee(s): ieee = [t.uint8_t(p, base=16) for p in s.split(b':')] return t.EUI64(ieee)
def zdo_f(app): ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) dev = zigpy.device.Device(app, ieee, 65535) dev.request = AsyncMock() return zdo.ZDO(dev)
def ieee(init=0): return t.EUI64(map(t.uint8_t, range(init, init + 8)))
async def test_start_network_no_api_mode(app): await _test_start_network(app, ai_status=0x00, api_mode=False) assert app.state.node_info.nwk == 0x0000 assert app.state.node_info.ieee == t.EUI64(range(1, 9)) assert app._api.init_api_mode.call_count == 1 assert app._api._at_command.call_count >= 16