示例#1
0
 def success():
     """Signal successful setup."""
     conf = load_json(hass.config.path(CONFIG_FILE))
     conf[host] = {CONF_API_KEY: api_key}
     save_json(hass.config.path(CONFIG_FILE), conf)
     req_config = _CONFIGURING.pop(host)
     configurator.request_done(hass, req_config)
示例#2
0
    def test_request_done_works(self):
        """Test if calling request done works."""
        request_id = configurator.request_config(
            self.hass, "Test Request", lambda _: None)
        configurator.request_done(request_id)
        self.assertEqual(1, len(self.hass.states.all()))

        self.hass.bus.fire(EVENT_TIME_CHANGED)
        self.hass.block_till_done()
        self.assertEqual(0, len(self.hass.states.all()))
示例#3
0
def setup_gpmdp(hass, config, code, add_entities):
    """Set up gpmdp."""
    name = config.get(CONF_NAME)
    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    url = f"ws://{host}:{port}"

    if not code:
        request_configuration(hass, config, url, add_entities)
        return

    if "gpmdp" in _CONFIGURING:
        configurator.request_done(hass, _CONFIGURING.pop("gpmdp"))

    add_entities([GPMDP(name, url, code)], True)
示例#4
0
    def register_account_callback(_):
        """Call for register the configurator."""
        api.retrieve_token(frob)
        token = api.token
        if api.token is None:
            _LOGGER.error("Failed to register, please try again")
            configurator.notify_errors(
                hass, request_id, "Failed to register, please try again.")
            return

        stored_rtm_config.set_token(account_name, token)
        _LOGGER.debug("Retrieved new token from server")

        _create_instance(
            hass,
            account_name,
            api_key,
            shared_secret,
            token,
            stored_rtm_config,
            component,
        )

        configurator.request_done(hass, request_id)
示例#5
0
 def test_request_done_fail_silently_on_bad_request_id(self):
     """Test that request_done fails silently with a bad request id."""
     configurator.request_done(2016)
示例#6
0
def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the Fitbit sensor."""
    config_path = hass.config.path(FITBIT_CONFIG_FILE)
    if os.path.isfile(config_path):
        config_file: ConfigType = cast(ConfigType, load_json(config_path))
        if config_file == DEFAULT_CONFIG:
            request_app_setup(hass,
                              config,
                              add_entities,
                              config_path,
                              discovery_info=None)
            return
    else:
        save_json(config_path, DEFAULT_CONFIG)
        request_app_setup(hass,
                          config,
                          add_entities,
                          config_path,
                          discovery_info=None)
        return

    if "fitbit" in _CONFIGURING:
        configurator.request_done(hass, _CONFIGURING.pop("fitbit"))

    access_token: str | None = config_file.get(ATTR_ACCESS_TOKEN)
    refresh_token: str | None = config_file.get(ATTR_REFRESH_TOKEN)
    expires_at: int | None = config_file.get(ATTR_LAST_SAVED_AT)
    if (access_token is not None and refresh_token is not None
            and expires_at is not None):
        authd_client = Fitbit(
            config_file.get(CONF_CLIENT_ID),
            config_file.get(CONF_CLIENT_SECRET),
            access_token=access_token,
            refresh_token=refresh_token,
            expires_at=expires_at,
            refresh_cb=lambda x: None,
        )

        if int(time.time()) - expires_at > 3600:
            authd_client.client.refresh_token()

        if (unit_system := config[CONF_UNIT_SYSTEM]) == "default":
            authd_client.system = authd_client.user_profile_get(
            )["user"]["locale"]
            if authd_client.system != "en_GB":
                if hass.config.units.is_metric:
                    authd_client.system = "metric"
                else:
                    authd_client.system = "en_US"
        else:
            authd_client.system = unit_system

        registered_devs = authd_client.get_devices()
        clock_format = config[CONF_CLOCK_FORMAT]
        monitored_resources = config[CONF_MONITORED_RESOURCES]
        entities = [
            FitbitSensor(
                authd_client,
                config_path,
                description,
                hass.config.units.is_metric,
                clock_format,
            ) for description in FITBIT_RESOURCES_LIST
            if description.key in monitored_resources
        ]
        if "devices/battery" in monitored_resources:
            entities.extend([
                FitbitSensor(
                    authd_client,
                    config_path,
                    FITBIT_RESOURCE_BATTERY,
                    hass.config.units.is_metric,
                    clock_format,
                    dev_extra,
                ) for dev_extra in registered_devs
            ])
        add_entities(entities, True)