Esempio n. 1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Goal Zero Yeti from a config entry."""
    name = entry.data[CONF_NAME]
    host = entry.data[CONF_HOST]

    api = Yeti(host, async_get_clientsession(hass))
    try:
        await api.init_connect()
    except exceptions.ConnectError as ex:
        raise ConfigEntryNotReady(f"Failed to connect to device: {ex}") from ex

    async def async_update_data() -> None:
        """Fetch data from API endpoint."""
        try:
            await api.get_state()
        except exceptions.ConnectError as err:
            raise UpdateFailed("Failed to communicate with device") from err

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        name=name,
        update_method=async_update_data,
        update_interval=MIN_TIME_BETWEEN_UPDATES,
    )
    await coordinator.async_config_entry_first_refresh()
    hass.data.setdefault(DOMAIN, {})
    hass.data[DOMAIN][entry.entry_id] = {
        DATA_KEY_API: api,
        DATA_KEY_COORDINATOR: coordinator,
    }

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
Esempio n. 2
0
 async def _async_try_connect(self, host: str) -> tuple[str | None, str | None]:
     """Try connecting to Goal Zero Yeti."""
     try:
         api = Yeti(host, async_get_clientsession(self.hass))
         await api.sysinfo()
     except exceptions.ConnectError:
         return None, "cannot_connect"
     except exceptions.InvalidHost:
         return None, "invalid_host"
     except Exception:  # pylint: disable=broad-except
         _LOGGER.exception("Unexpected exception")
         return None, "unknown"
     return str(api.sysdata["macAddress"]), None
Esempio n. 3
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Goal Zero Yeti from a config entry."""
    api = Yeti(entry.data[CONF_HOST], async_get_clientsession(hass))
    try:
        await api.init_connect()
    except exceptions.ConnectError as ex:
        raise ConfigEntryNotReady(f"Failed to connect to device: {ex}") from ex

    coordinator = GoalZeroDataUpdateCoordinator(hass, api)
    await coordinator.async_config_entry_first_refresh()
    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
Esempio n. 4
0
 async def _async_try_connect(self, host) -> tuple:
     """Try connecting to Goal Zero Yeti."""
     try:
         session = async_get_clientsession(self.hass)
         api = Yeti(host, self.hass.loop, session)
         await api.sysinfo()
     except exceptions.ConnectError:
         _LOGGER.error("Error connecting to device at %s", host)
         return None, "cannot_connect"
     except exceptions.InvalidHost:
         _LOGGER.error("Invalid host at %s", host)
         return None, "invalid_host"
     except Exception:  # pylint: disable=broad-except
         _LOGGER.exception("Unexpected exception")
         return None, "unknown"
     return str(api.sysdata["macAddress"]), None
Esempio n. 5
0
async def async_setup_entry(hass, entry):
    """Set up Goal Zero Yeti from a config entry."""
    name = entry.data[CONF_NAME]
    host = entry.data[CONF_HOST]

    _LOGGER.debug("Setting up %s integration with host %s", DOMAIN, host)

    session = async_get_clientsession(hass)
    api = Yeti(host, hass.loop, session)
    try:
        await api.get_state()
    except exceptions.ConnectError as ex:
        _LOGGER.warning("Failed to connect: %s", ex)
        raise ConfigEntryNotReady from ex

    async def async_update_data():
        """Fetch data from API endpoint."""
        try:
            await api.get_state()
        except exceptions.ConnectError as err:
            _LOGGER.warning("Failed to update data from Yeti")
            raise UpdateFailed(
                f"Failed to communicating with API: {err}") from err

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        name=name,
        update_method=async_update_data,
        update_interval=MIN_TIME_BETWEEN_UPDATES,
    )
    hass.data[DOMAIN][entry.entry_id] = {
        DATA_KEY_API: api,
        DATA_KEY_COORDINATOR: coordinator,
    }

    for platform in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, platform))

    return True
Esempio n. 6
0
async def async_setup_entry(opp, entry):
    """Set up Goal Zero Yeti from a config entry."""
    name = entry.data[CONF_NAME]
    host = entry.data[CONF_HOST]

    session = async_get_clientsession(opp)
    api = Yeti(host, opp.loop, session)
    try:
        await api.init_connect()
    except exceptions.ConnectError as ex:
        _LOGGER.warning("Failed to connect: %s", ex)
        raise ConfigEntryNotReady from ex

    async def async_update_data():
        """Fetch data from API endpoint."""
        try:
            await api.get_state()
        except exceptions.ConnectError as err:
            raise UpdateFailed(
                f"Failed to communicating with device {err}") from err

    coordinator = DataUpdateCoordinator(
        opp,
        _LOGGER,
        name=name,
        update_method=async_update_data,
        update_interval=MIN_TIME_BETWEEN_UPDATES,
    )
    opp.data.setdefault(DOMAIN, {})
    opp.data[DOMAIN][entry.entry_id] = {
        DATA_KEY_API: api,
        DATA_KEY_COORDINATOR: coordinator,
    }

    opp.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
 async def _async_try_connect(self, host):
     session = async_get_clientsession(self.hass)
     api = Yeti(host, self.hass.loop, session)
     await api.get_state()