mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
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:
parent
408af6e842
commit
10b120f11f
@ -35,7 +35,7 @@ def setup(hass, config):
|
|||||||
|
|
||||||
global BLOOMSKY
|
global BLOOMSKY
|
||||||
try:
|
try:
|
||||||
BLOOMSKY = BloomSky(api_key)
|
BLOOMSKY = BloomSky(api_key, hass.config.units.is_metric)
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -51,10 +51,12 @@ class BloomSky:
|
|||||||
# API documentation at http://weatherlution.com/bloomsky-api/
|
# API documentation at http://weatherlution.com/bloomsky-api/
|
||||||
API_URL = 'http://api.bloomsky.com/api/skydata'
|
API_URL = 'http://api.bloomsky.com/api/skydata'
|
||||||
|
|
||||||
def __init__(self, api_key):
|
def __init__(self, api_key, is_metric):
|
||||||
"""Initialize the BookSky."""
|
"""Initialize the BookSky."""
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
|
self._endpoint_argument = 'unit=intl' if is_metric else ''
|
||||||
self.devices = {}
|
self.devices = {}
|
||||||
|
self.is_metric = is_metric
|
||||||
_LOGGER.debug("Initial BloomSky device load...")
|
_LOGGER.debug("Initial BloomSky device load...")
|
||||||
self.refresh_devices()
|
self.refresh_devices()
|
||||||
|
|
||||||
@ -63,9 +65,13 @@ class BloomSky:
|
|||||||
"""Use the API to retrieve a list of devices."""
|
"""Use the API to retrieve a list of devices."""
|
||||||
_LOGGER.debug("Fetching BloomSky update")
|
_LOGGER.debug("Fetching BloomSky update")
|
||||||
response = requests.get(
|
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:
|
if response.status_code == 401:
|
||||||
raise RuntimeError("Invalid API_KEY")
|
raise RuntimeError("Invalid API_KEY")
|
||||||
|
if response.status_code == 405:
|
||||||
|
_LOGGER.error("You have no bloomsky devices configured")
|
||||||
|
return
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
_LOGGER.error("Invalid HTTP response: %s", response.status_code)
|
_LOGGER.error("Invalid HTTP response: %s", response.status_code)
|
||||||
return
|
return
|
||||||
|
@ -4,7 +4,9 @@ import logging
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
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
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
@ -21,12 +23,19 @@ SENSOR_TYPES = ['Temperature',
|
|||||||
'Voltage']
|
'Voltage']
|
||||||
|
|
||||||
# Sensor units - these do not currently align with the API documentation
|
# Sensor units - these do not currently align with the API documentation
|
||||||
SENSOR_UNITS = {'Temperature': TEMP_FAHRENHEIT,
|
SENSOR_UNITS_IMPERIAL = {'Temperature': TEMP_FAHRENHEIT,
|
||||||
'Humidity': '%',
|
'Humidity': '%',
|
||||||
'Pressure': 'inHg',
|
'Pressure': 'inHg',
|
||||||
'Luminance': 'cd/m²',
|
'Luminance': 'cd/m²',
|
||||||
'Voltage': 'mV'}
|
'Voltage': 'mV'}
|
||||||
|
|
||||||
|
# Metric units
|
||||||
|
SENSOR_UNITS_METRIC = {'Temperature': TEMP_CELSIUS,
|
||||||
|
'Humidity': '%',
|
||||||
|
'Pressure': 'mbar',
|
||||||
|
'Luminance': 'cd/m²',
|
||||||
|
'Voltage': 'mV'}
|
||||||
|
|
||||||
# Which sensors to format numerically
|
# Which sensors to format numerically
|
||||||
FORMAT_NUMBERS = ['Temperature', 'Pressure', 'Voltage']
|
FORMAT_NUMBERS = ['Temperature', 'Pressure', 'Voltage']
|
||||||
|
|
||||||
@ -77,7 +86,9 @@ class BloomSkySensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the sensor units."""
|
"""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):
|
def update(self):
|
||||||
"""Request an update from the BloomSky API."""
|
"""Request an update from the BloomSky API."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user