예제 #1
0
def main():
    """Print out information about all discovered devices."""
    try:
        discover()
        print(f"Discovered devices: {'(None)' if not devices else ''}")
        for device in devices.values():
            print(device)
    finally:
        cleanup()
예제 #2
0
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
    """Initialize the numato integration.

    Discovers available Numato devices and loads the binary_sensor, sensor and
    switch platforms.

    Returns False on error during device discovery (e.g. duplicate ID),
    otherwise returns True.

    No exceptions should occur, since the platforms are initialized on a best
    effort basis, which means, errors are handled locally.
    """
    hass.data[DOMAIN] = config[DOMAIN]

    try:
        gpio.discover(config[DOMAIN][CONF_DISCOVER])
    except gpio.NumatoGpioError as err:
        _LOGGER.info("Error discovering Numato devices: %s", err)
        gpio.cleanup()
        return False

    _LOGGER.info(
        "Initializing Numato 32 port USB GPIO expanders with IDs: %s",
        ", ".join(str(d) for d in gpio.devices),
    )

    hass.data[DOMAIN][DATA_API] = NumatoAPI()

    def cleanup_gpio(event):
        """Stuff to do before stopping."""
        _LOGGER.debug("Clean up Numato GPIO")
        gpio.cleanup()
        if DATA_API in hass.data[DOMAIN]:
            hass.data[DOMAIN][DATA_API].ports_registered.clear()

    def prepare_gpio(event):
        """Stuff to do when home assistant starts."""
        _LOGGER.debug("Setup cleanup at stop for Numato GPIO")
        hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, cleanup_gpio)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_START, prepare_gpio)

    load_platform(hass, Platform.BINARY_SENSOR, DOMAIN, {}, config)
    load_platform(hass, Platform.SENSOR, DOMAIN, {}, config)
    load_platform(hass, Platform.SWITCH, DOMAIN, {}, config)
    return True
예제 #3
0
def setup(opp, config):
    """Initialize the numato integration.

    Discovers available Numato devices and loads the binary_sensor, sensor and
    switch platforms.

    Returns False on error during device discovery (e.g. duplicate ID),
    otherwise returns True.

    No exceptions should occur, since the platforms are initialized on a best
    effort basis, which means, errors are handled locally.
    """
    opp.data[DOMAIN] = config[DOMAIN]

    try:
        gpio.discover(config[DOMAIN][CONF_DISCOVER])
    except gpio.NumatoGpioError as err:
        _LOGGER.info("Error discovering Numato devices: %s", err)
        gpio.cleanup()
        return False

    _LOGGER.info(
        "Initializing Numato 32 port USB GPIO expanders with IDs: %s",
        ", ".join(str(d) for d in gpio.devices),
    )

    opp.data[DOMAIN][DATA_API] = NumatoAPI()

    def cleanup_gpio(event):
        """Stuff to do before stopping."""
        _LOGGER.debug("Clean up Numato GPIO")
        gpio.cleanup()
        if DATA_API in opp.data[DOMAIN]:
            opp.data[DOMAIN][DATA_API].ports_registered.clear()

    def prepare_gpio(event):
        """Stuff to do when open peer power starts."""
        _LOGGER.debug("Setup cleanup at stop for Numato GPIO")
        opp.bus.listen_once(EVENT_OPENPEERPOWER_STOP, cleanup_gpio)

    opp.bus.listen_once(EVENT_OPENPEERPOWER_START, prepare_gpio)

    load_platform(opp, "binary_sensor", DOMAIN, {}, config)
    load_platform(opp, "sensor", DOMAIN, {}, config)
    load_platform(opp, "switch", DOMAIN, {}, config)
    return True
예제 #4
0
def test_nothing_discovered():
    gpio.discover(["/dev/__notexisting__"])
    assert not gpio.devices
예제 #5
0
def test_regular_discover_return_none_device_found():
    assert gpio.discover(["/dev/ttyACM0"]) is None
    assert gpio.devices