Ejemplo n.º 1
0
async def get_coap_context(hass):
    """Get CoAP context to be used in all Shelly devices."""
    context = aioshelly.COAP()
    await context.initialize()

    @callback
    def shutdown_listener(ev):
        context.close()

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_listener)

    return context
Ejemplo n.º 2
0
async def get_coap_context(opp):
    """Get CoAP context to be used in all Shelly devices."""
    context = aioshelly.COAP()
    if DOMAIN in opp.data:
        port = opp.data[DOMAIN].get(CONF_COAP_PORT, DEFAULT_COAP_PORT)
    else:
        port = DEFAULT_COAP_PORT
    _LOGGER.info("Starting CoAP context with UDP port %s", port)
    await context.initialize(port)

    @callback
    def shutdown_listener(ev):
        context.close()

    opp.bus.async_listen_once(EVENT_OPENPEERPOWER_STOP, shutdown_listener)

    return context
Ejemplo n.º 3
0
async def test_single(ip, username, password, init, timeout):
    options = aioshelly.ConnectionOptions(ip, username, password)

    async with aiohttp.ClientSession() as aiohttp_session, aioshelly.COAP(
    ) as coap_context:
        try:
            device = await asyncio.wait_for(
                aioshelly.Device.create(aiohttp_session, coap_context, options,
                                        init),
                timeout,
            )
        except asyncio.TimeoutError:
            print("Timeout connecting to", ip)
            return

        print_device(device)

        device.subscribe_updates(device_updated)

        while True:
            await asyncio.sleep(0.1)
Ejemplo n.º 4
0
async def test_devices(init, timeout):
    device_options = []
    with open("devices.json") as fp:
        for line in fp:
            device_options.append(
                aioshelly.ConnectionOptions(**json.loads(line)))

    async with aiohttp.ClientSession() as aiohttp_session, aioshelly.COAP(
    ) as coap_context:
        results = await asyncio.gather(
            *[
                asyncio.wait_for(
                    connect_and_print_device(aiohttp_session, coap_context,
                                             options, init),
                    timeout,
                ) for options in device_options
            ],
            return_exceptions=True,
        )

        for options, result in zip(device_options, results):
            if not isinstance(result, Exception):
                continue

            print()
            print(f"Error printing device @ {options.ip_address}")

            if isinstance(result, asyncio.TimeoutError):
                print("Timeout connecting to device")
            else:
                print()
                traceback.print_tb(result.__traceback__)
                print(result)

        while True:
            await asyncio.sleep(0.1)