Fix bloomsky unit system (#25460)

* initial commit - fix bloomsky unit system

* Add another warning

* Fix linting error

* Include metric sensor units

* Shorten a too long line
This commit is contained in:
Santobert 2019-07-24 19:37:36 +02:00 committed by Martin Hjelmare
parent 408af6e842
commit 10b120f11f
2 changed files with 27 additions and 10 deletions

View File

@ -35,7 +35,7 @@ def setup(hass, config):
global BLOOMSKY
try:
BLOOMSKY = BloomSky(api_key)
BLOOMSKY = BloomSky(api_key, hass.config.units.is_metric)
except RuntimeError:
return False
@ -51,10 +51,12 @@ class BloomSky:
# API documentation at http://weatherlution.com/bloomsky-api/
API_URL = 'http://api.bloomsky.com/api/skydata'
def __init__(self, api_key):
def __init__(self, api_key, is_metric):
"""Initialize the BookSky."""
self._api_key = api_key
self._endpoint_argument = 'unit=intl' if is_metric else ''
self.devices = {}
self.is_metric = is_metric
_LOGGER.debug("Initial BloomSky device load...")
self.refresh_devices()
@ -63,9 +65,13 @@ class BloomSky:
"""Use the API to retrieve a list of devices."""
_LOGGER.debug("Fetching BloomSky update")
response = requests.get(
self.API_URL, headers={AUTHORIZATION: self._api_key}, timeout=10)
"{}?{}".format(self.API_URL, self._endpoint_argument),
headers={AUTHORIZATION: self._api_key}, timeout=10)
if response.status_code == 401:
raise RuntimeError("Invalid API_KEY")
if response.status_code == 405:
_LOGGER.error("You have no bloomsky devices configured")
return
if response.status_code != 200:
_LOGGER.error("Invalid HTTP response: %s", response.status_code)
return

View File

@ -4,7 +4,9 @@ import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (TEMP_FAHRENHEIT, CONF_MONITORED_CONDITIONS)
from homeassistant.const import (TEMP_FAHRENHEIT,
TEMP_CELSIUS,
CONF_MONITORED_CONDITIONS)
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
@ -21,11 +23,18 @@ SENSOR_TYPES = ['Temperature',
'Voltage']
# Sensor units - these do not currently align with the API documentation
SENSOR_UNITS = {'Temperature': TEMP_FAHRENHEIT,
'Humidity': '%',
'Pressure': 'inHg',
'Luminance': 'cd/m²',
'Voltage': 'mV'}
SENSOR_UNITS_IMPERIAL = {'Temperature': TEMP_FAHRENHEIT,
'Humidity': '%',
'Pressure': 'inHg',
'Luminance': 'cd/m²',
'Voltage': 'mV'}
# Metric units
SENSOR_UNITS_METRIC = {'Temperature': TEMP_CELSIUS,
'Humidity': '%',
'Pressure': 'mbar',
'Luminance': 'cd/m²',
'Voltage': 'mV'}
# Which sensors to format numerically
FORMAT_NUMBERS = ['Temperature', 'Pressure', 'Voltage']
@ -77,7 +86,9 @@ class BloomSkySensor(Entity):
@property
def unit_of_measurement(self):
"""Return the sensor units."""
return SENSOR_UNITS.get(self._sensor_name, None)
if self._bloomsky.is_metric:
return SENSOR_UNITS_METRIC.get(self._sensor_name, None)
return SENSOR_UNITS_IMPERIAL.get(self._sensor_name, None)
def update(self):
"""Request an update from the BloomSky API."""