diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index 7343c2d21ed..0f35cca5f08 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -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), diff --git a/homeassistant/components/arwn/sensor.py b/homeassistant/components/arwn/sensor.py index 7be5c4bfb93..334c086f8d6 100644 --- a/homeassistant/components/arwn/sensor.py +++ b/homeassistant/components/arwn/sensor.py @@ -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"), ) diff --git a/homeassistant/components/buienradar/sensor.py b/homeassistant/components/buienradar/sensor.py index de7dd04a48e..8ace9070f8c 100644 --- a/homeassistant/components/buienradar/sensor.py +++ b/homeassistant/components/buienradar/sensor.py @@ -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], diff --git a/homeassistant/components/darksky/sensor.py b/homeassistant/components/darksky/sensor.py index d372811edd8..e4471b7e55f 100644 --- a/homeassistant/components/darksky/sensor.py +++ b/homeassistant/components/darksky/sensor.py @@ -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"], ], diff --git a/homeassistant/components/homematic/sensor.py b/homeassistant/components/homematic/sensor.py index d36b05fd129..13e49bcf509 100644 --- a/homeassistant/components/homematic/sensor.py +++ b/homeassistant/components/homematic/sensor.py @@ -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", diff --git a/homeassistant/components/isy994/sensor.py b/homeassistant/components/isy994/sensor.py index a52dcdbe09a..d3a889902f5 100644 --- a/homeassistant/components/isy994/sensor.py +++ b/homeassistant/components/isy994/sensor.py @@ -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 = { diff --git a/homeassistant/components/lcn/const.py b/homeassistant/components/lcn/const.py index 7a881ab9d85..817b6821db1 100644 --- a/homeassistant/components/lcn/const.py +++ b/homeassistant/components/lcn/const.py @@ -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"] diff --git a/homeassistant/components/mysensors/sensor.py b/homeassistant/components/mysensors/sensor.py index 16c8d37f447..ac16198ee69 100644 --- a/homeassistant/components/mysensors/sensor.py +++ b/homeassistant/components/mysensors/sensor.py @@ -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], diff --git a/homeassistant/components/openweathermap/sensor.py b/homeassistant/components/openweathermap/sensor.py index ac85eff6794..b5b9aa25e47 100644 --- a/homeassistant/components/openweathermap/sensor.py +++ b/homeassistant/components/openweathermap/sensor.py @@ -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], diff --git a/homeassistant/components/smappee/sensor.py b/homeassistant/components/smappee/sensor.py index 5948f7e4223..d82e63a2614 100644 --- a/homeassistant/components/smappee/sensor.py +++ b/homeassistant/components/smappee/sensor.py @@ -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": [ diff --git a/homeassistant/components/torque/sensor.py b/homeassistant/components/torque/sensor.py index fd7eddbb48a..7b7f7d7ec16 100644 --- a/homeassistant/components/torque/sensor.py +++ b/homeassistant/components/torque/sensor.py @@ -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: diff --git a/homeassistant/components/trafikverket_weatherstation/sensor.py b/homeassistant/components/trafikverket_weatherstation/sensor.py index f2e7387aa6b..6c678ab732c 100644 --- a/homeassistant/components/trafikverket_weatherstation/sensor.py +++ b/homeassistant/components/trafikverket_weatherstation/sensor.py @@ -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, diff --git a/homeassistant/components/wink/sensor.py b/homeassistant/components/wink/sensor.py index 2d0313ec211..1d2339702ad 100644 --- a/homeassistant/components/wink/sensor.py +++ b/homeassistant/components/wink/sensor.py @@ -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() diff --git a/homeassistant/components/wunderground/sensor.py b/homeassistant/components/wunderground/sensor.py index 6d843c222d9..b9532daa995 100644 --- a/homeassistant/components/wunderground/sensor.py +++ b/homeassistant/components/wunderground/sensor.py @@ -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" diff --git a/homeassistant/components/yr/sensor.py b/homeassistant/components/yr/sensor.py index e477f4a3c89..e648e059fab 100644 --- a/homeassistant/components/yr/sensor.py +++ b/homeassistant/components/yr/sensor.py @@ -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], diff --git a/homeassistant/components/zamg/sensor.py b/homeassistant/components/zamg/sensor.py index 0ead849b59a..6580ab0917f 100644 --- a/homeassistant/components/zamg/sensor.py +++ b/homeassistant/components/zamg/sensor.py @@ -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), diff --git a/homeassistant/const.py b/homeassistant/const.py index 0b8c3303358..99274d59ff7 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -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}" diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 66ee4d60bf3..699df2c3505 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -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() diff --git a/tests/components/yr/test_sensor.py b/tests/components/yr/test_sensor.py index 398acd7d554..b500a03c422 100644 --- a/tests/components/yr/test_sensor.py +++ b/tests/components/yr/test_sensor.py @@ -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")