Пример #1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Smappee from a zeroconf or config entry."""
    if CONF_IP_ADDRESS in entry.data:
        if helper.is_smappee_genius(entry.data[CONF_SERIALNUMBER]):
            # next generation: local mqtt broker
            smappee_mqtt = mqtt.SmappeeLocalMqtt(
                serial_number=entry.data[CONF_SERIALNUMBER])
            await hass.async_add_executor_job(
                smappee_mqtt.start_and_wait_for_config)
            smappee = Smappee(api=smappee_mqtt,
                              serialnumber=entry.data[CONF_SERIALNUMBER])
        else:
            # legacy devices through local api
            smappee_api = api.api.SmappeeLocalApi(
                ip=entry.data[CONF_IP_ADDRESS])
            smappee = Smappee(api=smappee_api,
                              serialnumber=entry.data[CONF_SERIALNUMBER])
        await hass.async_add_executor_job(smappee.load_local_service_location)
    else:
        implementation = (
            await
            config_entry_oauth2_flow.async_get_config_entry_implementation(
                hass, entry))

        smappee_api = api.ConfigEntrySmappeeApi(hass, entry, implementation)

        smappee = Smappee(api=smappee_api)
        await hass.async_add_executor_job(smappee.load_service_locations)

    hass.data[DOMAIN][entry.entry_id] = SmappeeBase(hass, smappee)

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
Пример #2
0
    async def async_step_zeroconf_confirm(self, user_input=None):
        """Confirm zeroconf flow."""
        errors = {}

        # Check if already configured (cloud)
        if self.is_cloud_device_already_added():
            return self.async_abort(reason="already_configured_device")

        if user_input is None:
            serialnumber = self.context.get(CONF_SERIALNUMBER)
            return self.async_show_form(
                step_id="zeroconf_confirm",
                description_placeholders={"serialnumber": serialnumber},
                errors=errors,
            )

        ip_address = self.context.get(CONF_IP_ADDRESS)
        serial_number = self.context.get(CONF_SERIALNUMBER)

        # Attempt to make a connection to the local device
        if helper.is_smappee_genius(serial_number):
            # next generation device, attempt connect to the local mqtt broker
            smappee_mqtt = mqtt.SmappeeLocalMqtt(serial_number=serial_number)
            connect = await self.opp.async_add_executor_job(
                smappee_mqtt.start_attempt)
            if not connect:
                return self.async_abort(reason="cannot_connect")
        else:
            # legacy devices, without local mqtt broker, try api access
            smappee_api = api.api.SmappeeLocalApi(ip=ip_address)
            logon = await self.opp.async_add_executor_job(smappee_api.logon)
            if logon is None:
                return self.async_abort(reason="cannot_connect")

        return self.async_create_entry(
            title=f"{DOMAIN}{serial_number}",
            data={
                CONF_IP_ADDRESS: ip_address,
                CONF_SERIALNUMBER: serial_number
            },
        )