Beispiel #1
0
async def async_setup_entry(hass, config_entry):
    """Set up a new config_entry for UPB PIM."""

    url = config_entry.data[CONF_HOST]
    file = config_entry.data[CONF_FILE_PATH]

    upb = upb_lib.UpbPim({"url": url, "UPStartExportFile": file})
    upb.connect()
    hass.data.setdefault(DOMAIN, {})
    hass.data[DOMAIN][config_entry.entry_id] = {"upb": upb}

    hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)

    def _element_changed(element, changeset):
        change = changeset.get("last_change")
        if change is None:
            return
        if change.get("command") is None:
            return

        hass.bus.async_fire(
            EVENT_UPB_SCENE_CHANGED,
            {
                ATTR_COMMAND: change["command"],
                ATTR_ADDRESS: element.addr.index,
                ATTR_BRIGHTNESS_PCT: change.get("level", -1),
                ATTR_RATE: change.get("rate", -1),
            },
        )

    for link in upb.links:
        element = upb.links[link]
        element.add_callback(_element_changed)

    return True
Beispiel #2
0
async def _validate_input(data):
    """Validate the user input allows us to connect."""

    def _connected_callback():
        connected_event.set()

    connected_event = asyncio.Event()
    file_path = data.get(CONF_FILE_PATH)
    url = _make_url_from_data(data)

    upb = upb_lib.UpbPim({"url": url, "UPStartExportFile": file_path})
    if not upb.config_ok:
        _LOGGER.error("Missing or invalid UPB file: %s", file_path)
        raise InvalidUpbFile

    upb.connect(_connected_callback)

    with suppress(asyncio.TimeoutError), async_timeout.timeout(VALIDATE_TIMEOUT):
        await connected_event.wait()

    upb.disconnect()

    if not connected_event.is_set():
        _LOGGER.error(
            "Timed out after %d seconds trying to connect with UPB PIM at %s",
            VALIDATE_TIMEOUT,
            url,
        )
        raise CannotConnect

    # Return info that you want to store in the config entry.
    return (upb.network_id, {"title": "UPB", CONF_HOST: url, CONF_FILE_PATH: file_path})
Beispiel #3
0
async def async_setup_entry(hass: HomeAssistant,
                            config_entry: ConfigEntry) -> bool:
    """Set up a new config_entry for UPB PIM."""

    url = config_entry.data[CONF_HOST]
    file = config_entry.data[CONF_FILE_PATH]

    upb = upb_lib.UpbPim({"url": url, "UPStartExportFile": file})
    upb.connect()
    hass.data.setdefault(DOMAIN, {})
    hass.data[DOMAIN][config_entry.entry_id] = {"upb": upb}

    await hass.config_entries.async_forward_entry_setups(
        config_entry, PLATFORMS)

    def _element_changed(element, changeset):
        if (change := changeset.get("last_change")) is None:
            return
        if change.get("command") is None:
            return

        hass.bus.async_fire(
            EVENT_UPB_SCENE_CHANGED,
            {
                ATTR_COMMAND: change["command"],
                ATTR_ADDRESS: element.addr.index,
                ATTR_BRIGHTNESS_PCT: change.get("level", -1),
                ATTR_RATE: change.get("rate", -1),
            },
        )
Beispiel #4
0
async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
    """Set up the UPB platform."""
    conf = hass_config[DOMAIN]

    upb = upb_lib.UpbPim(
        {
            "url": conf[CONF_URL],
            "UPStartExportFile": conf[CONF_FILE_PATH],
            "flags": conf[CONF_FLAGS],
        }
    )
    upb.connect()
    hass.data[DOMAIN] = {"upb": upb}

    init_entity_service(hass, DOMAIN)

    for component in ["light", "scene"]:
        hass.async_create_task(
            discovery.async_load_platform(hass, component, DOMAIN, {}, hass_config)
        )
    return True