Add and use UNIT_DEGREE constant (#33978)

* Add and use UNIT_DEGREE constant

* Replace more occurrences

* Add and use TEMP_KELVIN

* Run isort
This commit is contained in:
springstan 2020-04-11 16:54:11 +02:00 committed by GitHub
parent c18a6d5ea3
commit b893150154
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 67 additions and 47 deletions

View file

@ -16,6 +16,7 @@ from homeassistant.const import (
POWER_WATT,
SPEED_MILES_PER_HOUR,
TEMP_FAHRENHEIT,
UNIT_DEGREE,
UNIT_PERCENTAGE,
)
from homeassistant.core import callback
@ -218,10 +219,10 @@ SENSOR_TYPES = {
TYPE_TOTALRAININ: ("Lifetime Rain", "in", TYPE_SENSOR, None),
TYPE_UV: ("uv", "Index", TYPE_SENSOR, None),
TYPE_WEEKLYRAININ: ("Weekly Rain", "in", TYPE_SENSOR, None),
TYPE_WINDDIR: ("Wind Dir", "°", TYPE_SENSOR, None),
TYPE_WINDDIR_AVG10M: ("Wind Dir Avg 10m", "°", TYPE_SENSOR, None),
TYPE_WINDDIR: ("Wind Dir", UNIT_DEGREE, TYPE_SENSOR, None),
TYPE_WINDDIR_AVG10M: ("Wind Dir Avg 10m", UNIT_DEGREE, TYPE_SENSOR, None),
TYPE_WINDDIR_AVG2M: ("Wind Dir Avg 2m", SPEED_MILES_PER_HOUR, TYPE_SENSOR, None),
TYPE_WINDGUSTDIR: ("Gust Dir", "°", TYPE_SENSOR, None),
TYPE_WINDGUSTDIR: ("Gust Dir", UNIT_DEGREE, TYPE_SENSOR, None),
TYPE_WINDGUSTMPH: ("Wind Gust", SPEED_MILES_PER_HOUR, TYPE_SENSOR, None),
TYPE_WINDSPDMPH_AVG10M: ("Wind Avg 10m", SPEED_MILES_PER_HOUR, TYPE_SENSOR, None),
TYPE_WINDSPDMPH_AVG2M: ("Wind Avg 2m", SPEED_MILES_PER_HOUR, TYPE_SENSOR, None),

View file

@ -3,7 +3,7 @@ import json
import logging
from homeassistant.components import mqtt
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, UNIT_DEGREE
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify
@ -45,7 +45,7 @@ def discover_sensors(topic, payload):
return (
ArwnSensor("Wind Speed", "speed", unit, "mdi:speedometer"),
ArwnSensor("Wind Gust", "gust", unit, "mdi:speedometer"),
ArwnSensor("Wind Direction", "direction", "°", "mdi:compass"),
ArwnSensor("Wind Direction", "direction", UNIT_DEGREE, "mdi:compass"),
)

View file

@ -32,6 +32,7 @@ from homeassistant.const import (
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
TIME_HOURS,
UNIT_DEGREE,
UNIT_PERCENTAGE,
)
from homeassistant.core import callback
@ -76,7 +77,7 @@ SENSOR_TYPES = {
"windspeed": ["Wind speed", SPEED_KILOMETERS_PER_HOUR, "mdi:weather-windy"],
"windforce": ["Wind force", "Bft", "mdi:weather-windy"],
"winddirection": ["Wind direction", None, "mdi:compass-outline"],
"windazimuth": ["Wind direction azimuth", "°", "mdi:compass-outline"],
"windazimuth": ["Wind direction azimuth", UNIT_DEGREE, "mdi:compass-outline"],
"pressure": ["Pressure", "hPa", "mdi:gauge"],
"visibility": ["Visibility", LENGTH_KILOMETERS, None],
"windgust": ["Wind gust", SPEED_KILOMETERS_PER_HOUR, "mdi:weather-windy"],
@ -148,11 +149,11 @@ SENSOR_TYPES = {
"winddirection_3d": ["Wind direction 3d", None, "mdi:compass-outline"],
"winddirection_4d": ["Wind direction 4d", None, "mdi:compass-outline"],
"winddirection_5d": ["Wind direction 5d", None, "mdi:compass-outline"],
"windazimuth_1d": ["Wind direction azimuth 1d", "°", "mdi:compass-outline"],
"windazimuth_2d": ["Wind direction azimuth 2d", "°", "mdi:compass-outline"],
"windazimuth_3d": ["Wind direction azimuth 3d", "°", "mdi:compass-outline"],
"windazimuth_4d": ["Wind direction azimuth 4d", "°", "mdi:compass-outline"],
"windazimuth_5d": ["Wind direction azimuth 5d", "°", "mdi:compass-outline"],
"windazimuth_1d": ["Wind direction azimuth 1d", UNIT_DEGREE, "mdi:compass-outline"],
"windazimuth_2d": ["Wind direction azimuth 2d", UNIT_DEGREE, "mdi:compass-outline"],
"windazimuth_3d": ["Wind direction azimuth 3d", UNIT_DEGREE, "mdi:compass-outline"],
"windazimuth_4d": ["Wind direction azimuth 4d", UNIT_DEGREE, "mdi:compass-outline"],
"windazimuth_5d": ["Wind direction azimuth 5d", UNIT_DEGREE, "mdi:compass-outline"],
"condition_1d": ["Condition 1d", None, None],
"condition_2d": ["Condition 2d", None, None],
"condition_3d": ["Condition 3d", None, None],

View file

@ -22,6 +22,7 @@ from homeassistant.const import (
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
TIME_HOURS,
UNIT_DEGREE,
UNIT_PERCENTAGE,
UNIT_UV_INDEX,
)
@ -87,11 +88,11 @@ SENSOR_TYPES = {
],
"nearest_storm_bearing": [
"Nearest Storm Bearing",
"°",
"°",
"°",
"°",
"°",
UNIT_DEGREE,
UNIT_DEGREE,
UNIT_DEGREE,
UNIT_DEGREE,
UNIT_DEGREE,
"mdi:weather-lightning",
["currently"],
],
@ -177,11 +178,11 @@ SENSOR_TYPES = {
],
"wind_bearing": [
"Wind Bearing",
"°",
"°",
"°",
"°",
"°",
UNIT_DEGREE,
UNIT_DEGREE,
UNIT_DEGREE,
UNIT_DEGREE,
UNIT_DEGREE,
"mdi:compass",
["currently", "hourly", "daily"],
],

View file

@ -10,6 +10,7 @@ from homeassistant.const import (
POWER_WATT,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
UNIT_DEGREE,
UNIT_PERCENTAGE,
UNIT_VOLT,
VOLUME_CUBIC_METERS,
@ -53,8 +54,8 @@ HM_UNIT_HA_CAST = {
"HIGHEST_ILLUMINATION": "lx",
"RAIN_COUNTER": "mm",
"WIND_SPEED": SPEED_KILOMETERS_PER_HOUR,
"WIND_DIRECTION": "°",
"WIND_DIRECTION_RANGE": "°",
"WIND_DIRECTION": UNIT_DEGREE,
"WIND_DIRECTION_RANGE": UNIT_DEGREE,
"SUNSHINEDURATION": "#",
"AIR_PRESSURE": "hPa",
"FREQUENCY": "Hz",

View file

@ -19,6 +19,7 @@ from homeassistant.const import (
TIME_MONTHS,
TIME_SECONDS,
TIME_YEARS,
UNIT_DEGREE,
UNIT_PERCENTAGE,
UNIT_UV_INDEX,
UNIT_VOLT,
@ -41,7 +42,7 @@ UOM_FRIENDLY_NAME = {
"10": TIME_DAYS,
"12": "dB",
"13": "dB A",
"14": "°",
"14": UNIT_DEGREE,
"16": "macroseismic",
"17": TEMP_FAHRENHEIT,
"18": "ft",
@ -97,7 +98,7 @@ UOM_FRIENDLY_NAME = {
"73": POWER_WATT,
"74": f"{POWER_WATT}/m²",
"75": "weekday",
"76": "Wind Direction (°)",
"76": f"Wind Direction ({UNIT_DEGREE})",
"77": TIME_YEARS,
"82": "mm",
"83": LENGTH_KILOMETERS,
@ -107,8 +108,8 @@ UOM_FRIENDLY_NAME = {
"88": "Water activity",
"89": "RPM",
"90": "Hz",
"91": "° (Relative to North)",
"92": "° (Relative to South)",
"91": f"{UNIT_DEGREE} (Relative to North)",
"92": f"{UNIT_DEGREE} (Relative to South)",
}
UOM_TO_STATES = {

View file

@ -4,6 +4,8 @@ from itertools import product
from homeassistant.const import (
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
TEMP_KELVIN,
UNIT_DEGREE,
UNIT_PERCENTAGE,
UNIT_VOLT,
)
@ -149,7 +151,7 @@ VAR_UNITS = [
"LCN",
"NATIVE",
TEMP_CELSIUS,
"°K",
TEMP_KELVIN,
TEMP_FAHRENHEIT,
"LUX_T",
"LX_T",
@ -167,7 +169,7 @@ VAR_UNITS = [
"AMP",
"A",
"DEGREE",
"°",
UNIT_DEGREE,
]
RELVARREF = ["CURRENT", "PROG"]

View file

@ -7,6 +7,7 @@ from homeassistant.const import (
POWER_WATT,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UNIT_DEGREE,
UNIT_PERCENTAGE,
UNIT_VOLT,
)
@ -22,7 +23,7 @@ SENSORS = {
"V_RAINRATE": [None, "mdi:weather-rainy"],
"V_WIND": [None, "mdi:weather-windy"],
"V_GUST": [None, "mdi:weather-windy"],
"V_DIRECTION": ["°", "mdi:compass"],
"V_DIRECTION": [UNIT_DEGREE, "mdi:compass"],
"V_WEIGHT": [MASS_KILOGRAMS, "mdi:weight-kilogram"],
"V_DISTANCE": ["m", "mdi:ruler"],
"V_IMPEDANCE": ["ohm", None],

View file

@ -15,6 +15,7 @@ from homeassistant.const import (
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UNIT_DEGREE,
UNIT_PERCENTAGE,
)
import homeassistant.helpers.config_validation as cv
@ -36,7 +37,7 @@ SENSOR_TYPES = {
"weather": ["Condition", None],
"temperature": ["Temperature", None],
"wind_speed": ["Wind speed", SPEED_METERS_PER_SECOND],
"wind_bearing": ["Wind bearing", "°"],
"wind_bearing": ["Wind bearing", UNIT_DEGREE],
"humidity": ["Humidity", UNIT_PERCENTAGE],
"pressure": ["Pressure", "mbar"],
"clouds": ["Cloud coverage", UNIT_PERCENTAGE],

View file

@ -5,6 +5,7 @@ import logging
from homeassistant.const import (
ENERGY_KILO_WATT_HOUR,
POWER_WATT,
UNIT_DEGREE,
UNIT_PERCENTAGE,
UNIT_VOLT,
VOLUME_CUBIC_METERS,
@ -73,7 +74,7 @@ SENSOR_TYPES = {
"Water Sensor Temperature",
"mdi:temperature-celsius",
"water",
"°",
UNIT_DEGREE,
"temperature",
],
"water_sensor_humidity": [

View file

@ -6,7 +6,7 @@ import voluptuous as vol
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_EMAIL, CONF_NAME
from homeassistant.const import CONF_EMAIL, CONF_NAME, UNIT_DEGREE
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
@ -91,7 +91,7 @@ class TorqueReceiveDataView(HomeAssistantView):
temp_unit = data[key]
if "\\xC2\\xB0" in temp_unit:
temp_unit = temp_unit.replace("\\xC2\\xB0", "°")
temp_unit = temp_unit.replace("\\xC2\\xB0", UNIT_DEGREE)
units[pid] = temp_unit
elif is_value:

View file

@ -18,6 +18,7 @@ from homeassistant.const import (
DEVICE_CLASS_TEMPERATURE,
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
UNIT_DEGREE,
UNIT_PERCENTAGE,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -61,7 +62,7 @@ SENSOR_TYPES = {
],
"wind_direction": [
"Wind direction",
"°",
UNIT_DEGREE,
"winddirection",
"mdi:flag-triangle",
None,

View file

@ -3,7 +3,7 @@ import logging
import pywink
from homeassistant.const import TEMP_CELSIUS
from homeassistant.const import TEMP_CELSIUS, UNIT_DEGREE
from . import DOMAIN, WinkDevice
@ -48,7 +48,7 @@ class WinkSensorDevice(WinkDevice):
"""Initialize the Wink device."""
super().__init__(wink, hass)
self.capability = self.wink.capability()
if self.wink.unit() == "°":
if self.wink.unit() == UNIT_DEGREE:
self._unit_of_measurement = TEMP_CELSIUS
else:
self._unit_of_measurement = self.wink.unit()

View file

@ -26,6 +26,7 @@ from homeassistant.const import (
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UNIT_DEGREE,
UNIT_PERCENTAGE,
)
from homeassistant.exceptions import PlatformNotReady
@ -456,7 +457,7 @@ SENSOR_TYPES = {
),
"weather": WUCurrentConditionsSensorConfig("Weather Summary", "weather", None),
"wind_degrees": WUCurrentConditionsSensorConfig(
"Wind Degrees", "wind_degrees", "mdi:weather-windy", "°"
"Wind Degrees", "wind_degrees", "mdi:weather-windy", UNIT_DEGREE
),
"wind_dir": WUCurrentConditionsSensorConfig(
"Wind Direction", "wind_dir", "mdi:weather-windy"

View file

@ -24,6 +24,7 @@ from homeassistant.const import (
PRESSURE_HPA,
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
UNIT_DEGREE,
UNIT_PERCENTAGE,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -47,7 +48,7 @@ SENSOR_TYPES = {
"windSpeed": ["Wind speed", SPEED_METERS_PER_SECOND, None],
"windGust": ["Wind gust", SPEED_METERS_PER_SECOND, None],
"pressure": ["Pressure", PRESSURE_HPA, DEVICE_CLASS_PRESSURE],
"windDirection": ["Wind direction", "°", None],
"windDirection": ["Wind direction", UNIT_DEGREE, None],
"humidity": ["Humidity", UNIT_PERCENTAGE, DEVICE_CLASS_HUMIDITY],
"fog": ["Fog", UNIT_PERCENTAGE, None],
"cloudiness": ["Cloudiness", UNIT_PERCENTAGE, None],

View file

@ -19,6 +19,7 @@ from homeassistant.const import (
CONF_NAME,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
UNIT_DEGREE,
UNIT_PERCENTAGE,
__version__,
)
@ -48,14 +49,14 @@ SENSOR_TYPES = {
f"WG {SPEED_KILOMETERS_PER_HOUR}",
float,
),
"wind_bearing": ("Wind Bearing", "°", "WR °", int),
"wind_bearing": ("Wind Bearing", UNIT_DEGREE, f"WR {UNIT_DEGREE}", int),
"wind_max_speed": (
"Top Wind Speed",
SPEED_KILOMETERS_PER_HOUR,
f"WSG {SPEED_KILOMETERS_PER_HOUR}",
float,
),
"wind_max_bearing": ("Top Wind Bearing", "°", "WSR °", int),
"wind_max_bearing": ("Top Wind Bearing", UNIT_DEGREE, f"WSR {UNIT_DEGREE}", int),
"sun_last_hour": ("Sun Last Hour", UNIT_PERCENTAGE, f"SO {UNIT_PERCENTAGE}", int),
"temperature": ("Temperature", TEMP_CELSIUS, f"T {TEMP_CELSIUS}", float),
"precipitation": ("Precipitation", "l/m²", "N l/m²", float),

View file

@ -352,9 +352,13 @@ UNIT_VOLT = "V"
ENERGY_WATT_HOUR = f"{POWER_WATT}h"
ENERGY_KILO_WATT_HOUR = f"k{ENERGY_WATT_HOUR}"
# Degree units
UNIT_DEGREE = "°"
# Temperature units
TEMP_CELSIUS = "°C"
TEMP_FAHRENHEIT = "°F"
TEMP_CELSIUS = f"{UNIT_DEGREE}C"
TEMP_FAHRENHEIT = f"{UNIT_DEGREE}F"
TEMP_KELVIN = f"{UNIT_DEGREE}K"
# Time units
TIME_MICROSECONDS = "μs"
@ -410,6 +414,7 @@ UNIT_UV_INDEX: str = "UV index"
# Percentage units
UNIT_PERCENTAGE = "%"
# Irradiation units
IRRADIATION_WATTS_PER_SQUARE_METER = f"{POWER_WATT}/{AREA_SQUARE_METERS}"

View file

@ -9,6 +9,7 @@ from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
DEVICE_CLASS_POWER,
ENERGY_KILO_WATT_HOUR,
UNIT_DEGREE,
)
from homeassistant.setup import async_setup_component
@ -47,7 +48,7 @@ async def prometheus_client(loop, hass, hass_client):
sensor3.entity_id = "sensor.electricity_price"
await sensor3.async_update_ha_state()
sensor4 = DemoSensor(None, "Wind Direction", 25, None, "°", None)
sensor4 = DemoSensor(None, "Wind Direction", 25, None, UNIT_DEGREE, None)
sensor4.hass = hass
sensor4.entity_id = "sensor.wind_direction"
await sensor4.async_update_ha_state()

View file

@ -3,7 +3,7 @@ from datetime import datetime
from unittest.mock import patch
from homeassistant.bootstrap import async_setup_component
from homeassistant.const import SPEED_METERS_PER_SECOND, UNIT_PERCENTAGE
from homeassistant.const import SPEED_METERS_PER_SECOND, UNIT_DEGREE, UNIT_PERCENTAGE
import homeassistant.util.dt as dt_util
from tests.common import assert_setup_component, load_fixture
@ -59,7 +59,7 @@ async def test_custom_setup(hass, aioclient_mock):
assert state.state == "1009.3"
state = hass.states.get("sensor.yr_wind_direction")
assert state.attributes.get("unit_of_measurement") == "°"
assert state.attributes.get("unit_of_measurement") == UNIT_DEGREE
assert state.state == "103.6"
state = hass.states.get("sensor.yr_humidity")
@ -105,7 +105,7 @@ async def test_forecast_setup(hass, aioclient_mock):
assert state.state == "1008.3"
state = hass.states.get("sensor.yr_wind_direction")
assert state.attributes.get("unit_of_measurement") == "°"
assert state.attributes.get("unit_of_measurement") == UNIT_DEGREE
assert state.state == "148.9"
state = hass.states.get("sensor.yr_humidity")