Esempio n. 1
0
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
    """Set up the Agent component."""
    hass.data.setdefault(AGENT_DOMAIN, {})

    server_origin = config_entry.data[SERVER_URL]

    agent_client = Agent(server_origin, async_get_clientsession(hass))
    try:
        await agent_client.update()
    except AgentError as err:
        await agent_client.close()
        raise ConfigEntryNotReady from err

    if not agent_client.is_available:
        raise ConfigEntryNotReady

    await agent_client.get_devices()

    hass.data[AGENT_DOMAIN][config_entry.entry_id] = {CONNECTION: agent_client}

    device_registry = dr.async_get(hass)

    device_registry.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        identifiers={(AGENT_DOMAIN, agent_client.unique)},
        manufacturer="iSpyConnect",
        name=f"Agent {agent_client.name}",
        model="Agent DVR",
        sw_version=agent_client.version,
    )

    hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)

    return True
Esempio n. 2
0
    async def async_step_user(self, user_input=None):
        """Handle an Agent config flow."""
        errors = {}

        if user_input is not None:
            host = user_input[CONF_HOST]
            port = user_input[CONF_PORT]

            server_origin = generate_url(host, port)
            agent_client = Agent(server_origin,
                                 async_get_clientsession(self.hass))

            try:
                await agent_client.update()
            except AgentConnectionError:
                pass
            except AgentError:
                pass

            await agent_client.close()

            if agent_client.is_available:
                await self.async_set_unique_id(agent_client.unique)

                self._abort_if_unique_id_configured(
                    updates={
                        CONF_HOST: user_input[CONF_HOST],
                        CONF_PORT: user_input[CONF_PORT],
                        SERVER_URL: server_origin,
                    })

                self.device_config = {
                    CONF_HOST: host,
                    CONF_PORT: port,
                    SERVER_URL: server_origin,
                }

                return await self._create_entry(agent_client.name)

            errors["base"] = "device_unavailable"

        data = {
            vol.Required(CONF_HOST): str,
            vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
        }

        return self.async_show_form(
            step_id="user",
            description_placeholders=self.device_config,
            data_schema=vol.Schema(data),
            errors=errors,
        )
Esempio n. 3
0
async def async_setup_entry(hass, config_entry):
    """Set up the Agent component."""
    hass.data.setdefault(AGENT_DOMAIN, {})

    server_origin = config_entry.data[SERVER_URL]

    agent_client = Agent(server_origin, async_get_clientsession(hass))
    try:
        await agent_client.update()
    except AgentError:
        await agent_client.close()
        raise ConfigEntryNotReady

    if not agent_client.is_available:
        raise ConfigEntryNotReady

    await agent_client.get_devices()

    hass.data[AGENT_DOMAIN][config_entry.entry_id] = {CONNECTION: agent_client}

    device_registry = await dr.async_get_registry(hass)

    device_registry.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        identifiers={(AGENT_DOMAIN, agent_client.unique)},
        manufacturer="iSpyConnect",
        name=f"Agent {agent_client.name}",
        model="Agent DVR",
        sw_version=agent_client.version,
    )

    for forward in FORWARDS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(
                config_entry, forward))

    return True