From 1eb3181c14110c379ac15ea06f483eeca38da3e5 Mon Sep 17 00:00:00 2001 From: John Arild Berentsen Date: Fri, 20 May 2016 08:28:53 +0200 Subject: [PATCH] Fix fitbit KeyError (#2077) * Fix fitbit KeyError * Set units compared to temperature_unit * Pass true or false for is_metric --- homeassistant/components/sensor/fitbit.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/fitbit.py b/homeassistant/components/sensor/fitbit.py index 7d122f857d7..eb9e6fdc00d 100644 --- a/homeassistant/components/sensor/fitbit.py +++ b/homeassistant/components/sensor/fitbit.py @@ -10,7 +10,7 @@ import logging import datetime import time -from homeassistant.const import HTTP_OK +from homeassistant.const import HTTP_OK, TEMP_CELSIUS from homeassistant.util import Throttle from homeassistant.helpers.entity import Entity from homeassistant.loader import get_component @@ -236,7 +236,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): dev = [] for resource in config.get("monitored_resources", FITBIT_DEFAULT_RESOURCE_LIST): - dev.append(FitbitSensor(authd_client, config_path, resource)) + dev.append(FitbitSensor(authd_client, config_path, resource, + hass.config.temperature_unit == + TEMP_CELSIUS)) add_devices(dev) else: @@ -314,8 +316,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class FitbitSensor(Entity): """Implementation of a Fitbit sensor.""" - def __init__(self, client, config_path, resource_type): - """Initialize the Uber sensor.""" + def __init__(self, client, config_path, resource_type, is_metric): + """Initialize the Fitbit sensor.""" self.client = client self.config_path = config_path self.resource_type = resource_type @@ -328,7 +330,13 @@ class FitbitSensor(Entity): unit_type = FITBIT_RESOURCES_LIST[self.resource_type] if unit_type == "": split_resource = self.resource_type.split("/") - measurement_system = FITBIT_MEASUREMENTS[self.client.system] + try: + measurement_system = FITBIT_MEASUREMENTS[self.client.system] + except KeyError: + if is_metric: + measurement_system = FITBIT_MEASUREMENTS["metric"] + else: + measurement_system = FITBIT_MEASUREMENTS["en_US"] unit_type = measurement_system[split_resource[-1]] self._unit_of_measurement = unit_type self._state = 0