Fix fitbit KeyError (#2077)

* Fix fitbit KeyError

* Set units compared to temperature_unit

* Pass true or false for is_metric
This commit is contained in:
John Arild Berentsen 2016-05-20 08:28:53 +02:00 committed by Paulus Schoutsen
parent f7b401a20e
commit 1eb3181c14

View File

@ -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