Ejemplo n.º 1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up iotawatt from a config entry."""
    polling_interval = entry.options.get(CONF_SCAN_INTERVAL,
                                         DEFAULT_SCAN_INTERVAL)

    session = AsyncClient()
    if "username" in entry.data.keys():
        api = Iotawatt(
            entry.data["name"],
            entry.data["host"],
            session,
            entry.data["username"],
            entry.data["password"],
        )
    else:
        api = Iotawatt(
            entry.data["name"],
            entry.data["host"],
            session,
        )

    coordinator = IotawattUpdater(
        hass,
        api=api,
        name="IoTaWatt",
        update_interval=polling_interval,
    )

    await coordinator.async_refresh()

    if not coordinator.last_update_success:
        raise ConfigEntryNotReady

    hass.data[DOMAIN][entry.entry_id] = {
        COORDINATOR: coordinator,
        IOTAWATT_API: api,
    }

    for component in PLATFORMS:
        _LOGGER.info(f"Setting up platform: {component}")
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, component))

    return True
Ejemplo n.º 2
0
async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect.

    Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
    """
    session = AsyncClient()
    iotawatt = Iotawatt(
        data["name"], data["host"], session
    )
    try:
        is_connected = await iotawatt.connect()
        _LOGGER.debug("isConnected: %s", is_connected)
    except (KeyError, json.JSONDecodeError, httpx.HTTPError):
        raise CannotConnect

    if not is_connected:
        raise InvalidAuth

    # Return info that you want to store in the config entry.
    return {"title": data["name"]}
Ejemplo n.º 3
0
async def validate_input(hass: core.HomeAssistant,
                         data: dict[str, str]) -> dict[str, str]:
    """Validate the user input allows us to connect."""
    iotawatt = Iotawatt(
        "",
        data[CONF_HOST],
        httpx_client.get_async_client(hass),
        data.get(CONF_USERNAME),
        data.get(CONF_PASSWORD),
    )
    try:
        is_connected = await iotawatt.connect()
    except CONNECTION_ERRORS:
        return {"base": "cannot_connect"}
    except Exception:  # pylint: disable=broad-except
        _LOGGER.exception("Unexpected exception")
        return {"base": "unknown"}

    if not is_connected:
        return {"base": "invalid_auth"}

    return {}
Ejemplo n.º 4
0
async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect.

    Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
    """
    session = AsyncClient()
    iotawatt = Iotawatt(
        data["name"], data["ip_address"], session, data["username"], data["password"]
    )
    is_connected = await iotawatt.connect()
    _LOGGER.debug("isConnected: %s", is_connected)

    if not is_connected:
        raise CannotConnect

    # If you cannot connect:
    # throw CannotConnect
    # If the authentication is wrong:
    # InvalidAuth

    # Return info that you want to store in the config entry.
    return {"title": data["name"]}
Ejemplo n.º 5
0
    async def _async_update_data(self):
        """Fetch sensors from IoTaWatt device."""
        if self.api is None:
            api = Iotawatt(
                self.entry.title,
                self.entry.data[CONF_HOST],
                httpx_client.get_async_client(self.hass),
                self.entry.data.get(CONF_USERNAME),
                self.entry.data.get(CONF_PASSWORD),
            )
            try:
                is_authenticated = await api.connect()
            except CONNECTION_ERRORS as err:
                raise UpdateFailed("Connection failed") from err

            if not is_authenticated:
                raise UpdateFailed("Authentication error")

            self.api = api

        await self.api.update()
        return self.api.getSensors()