Ejemplo n.º 1
0
    def __init__(self, api: API, database_file=None):
        super().__init__(database_file=database_file)
        self._api = api
        api.set_application(self)

        self.discovering = False
        self.version = {}
Ejemplo n.º 2
0
async def main():
    # noinspection PyUnresolvedReferences
    import zhaquirks  # noqa: F401

    api = API()
    while True:
        try:
            await api.connect("auto")
            break
        except SerialException as e:
            print(e)
            await asyncio.sleep(2)

    db = "store.db"
    try:
        app = application.ControllerApplication(api, database_file=db)
    except KeyError:
        LOGGER.error("DB error, removing DB...")
        await asyncio.sleep(1)
        os.remove(db)
        app = application.ControllerApplication(api, database_file=db)

    testapp = TestApp()

    app.add_context_listener(testapp)

    LOGGER.info("STARTUP")
    await app.startup(auto_form=False)
    await app.form_network()

    await app.permit_ncp()
Ejemplo n.º 3
0
async def registerEndpoints(znp: API):
    LOGGER.debug("Register endpoints...")

    active_ep_response = znp.wait_for(CommandType.AREQ, Subsystem.ZDO,
                                      "activeEpRsp")
    asyncio.create_task(
        znp.request(Subsystem.ZDO, "activeEpReq", {
            "dstaddr": 0,
            "nwkaddrofinterest": 0
        }))
    active_ep = await active_ep_response.wait()

    for endpoint in Endpoints:
        if endpoint.endpoint in active_ep.payload["activeeplist"]:
            LOGGER.debug("Endpoint '%s' already registered", endpoint.endpoint)
        else:
            LOGGER.debug("Registering endpoint '%s'", endpoint.endpoint)
            await znp.request(Subsystem.AF, "register", vars(endpoint))
Ejemplo n.º 4
0
async def initialise(znp: API, version, options: NetworkOptions):
    await znp.request(Subsystem.SYS, 'resetReq',
                      {"type": Constants.SYS.resetType.SOFT})
    await znp.request(Subsystem.SYS, 'osalNvWrite', Items.startupOption(0x02))
    await znp.request(Subsystem.SYS, 'resetReq',
                      {"type": Constants.SYS.resetType.SOFT})
    await znp.request(
        Subsystem.SYS, 'osalNvWrite',
        Items.logicalType(Constants.ZDO.deviceLogicalType.COORDINATOR))
    await znp.request(Subsystem.SYS, 'osalNvWrite',
                      Items.networkKeyDistribute(options.networkKeyDistribute))
    await znp.request(Subsystem.SYS, 'osalNvWrite', Items.zdoDirectCb())
    await znp.request(Subsystem.SYS, 'osalNvWrite',
                      Items.channelList(options.channelList))
    await znp.request(Subsystem.SYS, 'osalNvWrite', Items.panID(options.panID))
    await znp.request(Subsystem.SYS, 'osalNvWrite',
                      Items.extendedPanID(options.extendedPanID))

    if version == ZnpVersion.zStack30x or version == ZnpVersion.zStack3x0:
        await znp.request(Subsystem.SYS, 'osalNvWrite',
                          Items.networkKey(options.networkKey))

        # Default link key is already OK for Z-Stack 3 ('ZigBeeAlliance09')
        channelMask = int.from_bytes(
            bytes(getChannelMask(options.channelList)), 'little')
        await znp.request(Subsystem.APP_CNF, 'bdbSetChannel', {
            "isPrimary": 0x1,
            "channel": channelMask
        })
        await znp.request(Subsystem.APP_CNF, 'bdbSetChannel', {
            "isPrimary": 0x0,
            "channel": 0x0
        })

        started = znp.wait_for(CommandType.AREQ, Subsystem.ZDO,
                               'stateChangeInd', {"state": 9}, 60000)
        await znp.request(Subsystem.APP_CNF, 'bdbStartCommissioning',
                          {"mode": 0x04})
        try:
            await started.wait()
        except:
            raise Exception(
                'Coordinator failed to start, probably the panID is already in use, try a different panID or channel'
            )

        await znp.request(Subsystem.APP_CNF, 'bdbStartCommissioning',
                          {"mode": 0x02})
    else:
        await znp.request(Subsystem.SAPI, 'writeConfiguration',
                          Items.networkKey(options.networkKey))
        await znp.request(Subsystem.SYS, 'osalNvWrite', Items.tcLinkKey())

    # expect status code 9 (= item created and initialized)
    await znp.request(Subsystem.SYS, 'osalNvItemInit',
                      Items.znpHasConfiguredInit(version), [0, 9])
    await znp.request(Subsystem.SYS, 'osalNvWrite',
                      Items.znpHasConfigured(version))
Ejemplo n.º 5
0
async def boot(znp: API):
    result = await znp.request(Subsystem.UTIL, "getDeviceInfo", {})

    if result.payload["devicestate"] != Common.devStates["ZB_COORD"]:
        LOGGER.debug("Start ZNP as coordinator...")
        started = znp.wait_for(CommandType.AREQ, Subsystem.ZDO,
                               "stateChangeInd", {"state": 9}, 60000)
        await znp.request(Subsystem.ZDO, "startupFromApp", {"startdelay": 100},
                          None, [0, 1])
        await started.wait()
        LOGGER.info("ZNP started as coordinator")
    else:
        LOGGER.info("ZNP is already started as coordinator")
Ejemplo n.º 6
0
async def boot(znp: API):
    result = await znp.request(Subsystem.UTIL, 'getDeviceInfo', {})

    if result.payload['devicestate'] != Common.devStates["ZB_COORD"]:
        LOGGER.debug('Start ZNP as coordinator...')
        started = znp.wait_for(CommandType.AREQ, Subsystem.ZDO,
                               'stateChangeInd', {"state": 9}, 60000)
        await znp.request(Subsystem.ZDO, 'startupFromApp', {"startdelay": 100},
                          [0, 1])
        await started.wait()
        LOGGER.debug('ZNP started as coordinator')
    else:
        LOGGER.debug('ZNP is already started as coordinator')
Ejemplo n.º 7
0
async def registerEndpoints(znp: API):
    LOGGER.debug('Register endpoints...')

    activeEpResponse = znp.wait_for(CommandType.AREQ, Subsystem.ZDO,
                                    'activeEpRsp')
    try:
        await znp.request(Subsystem.ZDO, 'activeEpReq', {
            "dstaddr": 0,
            "nwkaddrofinterest": 0
        })
    except Exception as e:
        LOGGER.debug(e)
    activeEp = await activeEpResponse.wait()

    for endpoint in Endpoints:
        if endpoint.endpoint in activeEp.payload["activeeplist"]:
            LOGGER.debug("Endpoint '%s' already registered", endpoint.endpoint)
        else:
            LOGGER.debug("Registering endpoint '%s'", endpoint.endpoint)
            await znp.request(Subsystem.AF, 'register', vars(endpoint))
Ejemplo n.º 8
0
async def main():
    # noinspection PyUnresolvedReferences
    import zhaquirks  # noqa: F401

    api = API()
    while True:
        try:
            await api.connect("/dev/ttyACM0")
            break
        except SerialException as e:
            print(e)
            await asyncio.sleep(2)

    db = "store.db"
    try:
        app = application.ControllerApplication(api, database_file=db)
    except KeyError:
        LOGGER.error("DB error, removing DB...")
        await asyncio.sleep(1)
        os.remove(db)
        app = application.ControllerApplication(api, database_file=db)

    await app.startup(auto_form=False)
    await app.form_network()
Ejemplo n.º 9
0
def app(monkeypatch, database_file=None):
    app = application.ControllerApplication(API(), database_file=database_file)
    return app
Ejemplo n.º 10
0
def app():
    app = application.ControllerApplication(APP_CONFIG)
    app._api = API(APP_CONFIG[config.CONF_DEVICE])
    app._api.set_application(app)
    return app
Ejemplo n.º 11
0
def app():
    app = application.ControllerApplication(APP_CONFIG)
    app._api = API(APP_CONFIG[config.CONF_DEVICE])
    app._api.set_application(app)
    app._semaphore = asyncio.Semaphore()
    return app