Fitbit Component to support multiple Users

Continuing the discussion from Fitbit - more than one tracker?:

Link to the component: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/sensor/fitbit.py

Github issue: https://github.com/home-assistant/home-assistant/issues/12032

Since it’s more of a feature request… also tagging @CCOSTAN who originally found issue with the current setup. Succinct example showing use of config filename. Longer example via Hue.

What I’m unsure on, is if Home Assistant has any other components or platforms that require multiple auth flows.

I created a branch which implements support for multiple trackers. I need to test it a bit more. If anybody is willing to give it a try, here is the branch:

2 Likes

How do you add the 2nd tracker?

Worked perfectly for me. So good it enticed me to sign up and post.

I took the Fitbit component from your branch and placed it into my custom_components. I them added the filename and name parameters to the config.

Thanks!

@chris6, could you give more details about the data you added to the config? I’ve copied the component now trying to work out how to do the config.

Sure, under sensors.yaml:

  - platform: fitbit
    name: "fitbit_chris"
    filename: "fitbit_chris.conf"
    clock_format: 24H
    unit_system: en_GB
    monitored_resources:
      - "activities/steps"
      - "activities/floors"

  - platform: fitbit
    name: "fitbit_jane_doe"
    filename: "fitbit_jane_doe.conf"
    clock_format: 24H
    unit_system: en_GB
    monitored_resources:
      - "activities/steps"
      - "activities/floors"
1 Like

How do you use this in a HassOS install? I don’t see that it was ever merged into core?

Just make sure i have things right here. I need to get the fitbit component from agners home assistant repo and add it to our own custom_components, setup the new configs per @chris6’s example, and then restart hass?

Hmm, I dont think its loading the custom_component for me because im getting this error:

Invalid config for [sensor.fitbit]: [name] is an invalid option for [sensor.fitbit]. Check: sensor.fitbit->name. (See ?, line ?). 
Invalid config for [sensor.fitbit]: [name] is an invalid option for [sensor.fitbit]. Check: sensor.fitbit->name. (See ?, line ?).

Also, im seeing the follow entry in logs:

Detected use of deprecated `base_url` property, use `homeassistant.helpers.network.get_url` method instead. Please report issue to the custom component author for fitbit using this method at custom_components/fitbit/sensor.py, line 316: hass.config.api.base_url, FITBIT_AUTH_CALLBACK_PATH
Detected use of deprecated `base_url` property, use `homeassistant.helpers.network.get_url` method instead. Please report issue to the custom component author for fitbit using this method at custom_components/fitbit/sensor.py, line 218: start_url = f"{hass.config.api.base_url}{FITBIT_AUTH_START}"

Trying to remember the exact steps I followed to get it to work, it was a while ago I did it! Install and setup the original Fitbit component. This gives you file called fitbit.conf in HA folder. I renamed it to fitbit_eoin.conf, I then added a link to that newly renamed file to configuration.yaml

- platform: fitbit
  name: "fitbit_eoin"
  filename: "fitbit_eoin.conf"
  clock_format: 12H
  monitored_resources:
    - "body/weight"
    - "activities/tracker/steps"
    - "devices/battery"
    - "activities/distance"
    - "activities/activityCalories"
    - "activities/minutesSedentary"
    - "activities/tracker/minutesVeryActive"
    - "activities/minutesLightlyActive"
    - "activities/heart"
    - "body/bmi"
    - "body/fat"
    - "body/weight"
    - "sleep/minutesAsleep"
    - "activities/tracker/calories"

I then copied the files from agners repo into custom_components and restarted. This resulted in HA detecting a new fitbit integration. I ran through the exact same steps as the original setup which again resulted in fitbit.conf being created. I renamed this one too fitbit_mahiri.conf and added the following to configuration.yaml

- platform: fitbit
  name: "fitbit_mhairi"
  filename: "fitbit_mhairi.conf"
  clock_format: 12H
  monitored_resources:
    - "body/weight"
    - "activities/tracker/steps"
    - "devices/battery"
    - "activities/distance"
    - "activities/activityCalories"
    - "activities/minutesSedentary"
    - "activities/tracker/minutesVeryActive"
    - "activities/minutesLightlyActive"
    - "activities/heart"
    - "body/bmi"
    - "body/fat"
    - "body/weight"
    - "sleep/minutesAsleep"
    - "activities/tracker/calories"

I suspect you could repeat this process as many times as you like if you want more than 2 connections. Since doing it I have data coming in for both fitbit accounts without any issues.

Ive updated the custom fitbit sensor.py to use the multiple accounts and also uses all the latest changes that home assistant did to the core fitbit component: https://gist.github.com/MACscr/e76c914e8a2e9d4ebdb8c1f59d082f98

Its not perfect yet (dont have my wifes account added yet, but my fitbit sensors are coming in as sensor.weight, so nothing custom to me and i see that being a problem). i had to update for it to work with the http external_url config of home assistant as base_url isnt supported anymore and i have a different internal/external ur (nginx proxy).

Did yours correctly setup a unique name for each fitbit sensor that is associated with the ‘name’ conf value or is it just doing something like sensor.weight and sensor.weight_2?

Hello agners,

Are you able to scrape info about foods/log/caloriesIn and foods/log/water?

It would be nice to have that data available in a sensor for automations that reminded if the alloted calorie intake for a day has been exceeded or if you need to drink more water to reach your goal.

Added this two lines to your sensor.py and they seem to have done the trick.

    "foods/log/caloriesIn": ["Calories Consumed", "cal", "fire"],
    "foods/log/water": ["Water", "milliliters", "water"],

im wondering if anyone got it working recently?
Im getting in logs on start up:

The following integrations and platforms could not be set up:

And when i check config i get this:

Invalid config for [sensor.fitbit]> : [name] is an invalid option for [sensor.fitbit]. Check: sensor.fitbit-
name. (See ?, line ?).
Invalid config for [sensor.fitbit]: [name] is an invalid option for [sensor.fitbit]. Check: sensor.fitbit->name. (See ?, line ?).

I have fitibt folder frm @agners repo in my custom_component folder. But it seems it doesnt work for some reason. Are there any other settings required to set it up?
Standard fitbit integratino for just one user works just fine

I’ve just set this up today and ran into the issue that you’ve described.
I seem to have sorted it by naming the new integration folder in custom_components fit_bit and then changed the platform in sensor config to fit_bit.
Hope that helps!

1 Like

Woohooo!! Thank you sooo much @BigManDave ! Worked for me with fit_bit tweak!

1 Like

Hi,

I’ve made a version that authorize several fitbit account (same behavior as @agners ) and that make use of external_url set in HA.
You can copy the custom component from my repo: https://github.com/jncanches/home_assistant_fitbit_sensor

It’s basically a merge between the current official fitbit sensor and @agners one but with some updates about the external_url (I use nginx reverse proxy and I cannot set internal_url same as external one)

Hope this helps !

NB: if you configure sensor with, for example, name: test, the integration will produce sensors prefixed by test_, for example sensor.test_steps

jncanches
I thought: I give it a try.
After succesfully adding my wife’s account, I could not register my own account on fitbit.

After the first step I got the warning: 500 Internal Server Error Server got itself in trouble.

After a second new retry, i got the callback file downloaded.
“Fitbit has been successfully authorized! You can close this window now!”
But it did not safe anything in my own config file, and I have to configure it. (again)

It did not work.
I will give it another try in the weekend!

André

Weird.
I had a different problem with the URL generated in the last step. The redirected URL parameter still contains the internal_url. Editing it in browser address bar with the external one does the job.

Hope some devs will review this integration to make it more reliable.