diff --git a/homeassistant/components/elkm1/__init__.py b/homeassistant/components/elkm1/__init__.py index 5c6fbf71738..946e40b7e23 100644 --- a/homeassistant/components/elkm1/__init__.py +++ b/homeassistant/components/elkm1/__init__.py @@ -15,6 +15,8 @@ from homeassistant.const import ( CONF_PASSWORD, CONF_TEMPERATURE_UNIT, CONF_USERNAME, + TEMP_CELSIUS, + TEMP_FAHRENHEIT, ) from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady @@ -23,6 +25,8 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import ConfigType from .const import ( + BARE_TEMP_CELSIUS, + BARE_TEMP_FAHRENHEIT, CONF_AREA, CONF_AUTO_CONFIGURE, CONF_COUNTER, @@ -119,7 +123,10 @@ DEVICE_SCHEMA = vol.Schema( vol.Optional(CONF_USERNAME, default=""): cv.string, vol.Optional(CONF_PASSWORD, default=""): cv.string, vol.Optional(CONF_AUTO_CONFIGURE, default=False): cv.boolean, - vol.Optional(CONF_TEMPERATURE_UNIT, default="F"): cv.temperature_unit, + # cv.temperature_unit will mutate 'C' -> '°C' and 'F' -> '°F' + vol.Optional( + CONF_TEMPERATURE_UNIT, default=BARE_TEMP_FAHRENHEIT + ): cv.temperature_unit, vol.Optional(CONF_AREA, default={}): DEVICE_SCHEMA_SUBDOMAIN, vol.Optional(CONF_COUNTER, default={}): DEVICE_SCHEMA_SUBDOMAIN, vol.Optional(CONF_KEYPAD, default={}): DEVICE_SCHEMA_SUBDOMAIN, @@ -187,7 +194,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): _LOGGER.debug("Setting up elkm1 %s", conf["host"]) - config = {"temperature_unit": conf[CONF_TEMPERATURE_UNIT]} + temperature_unit = TEMP_FAHRENHEIT + if conf[CONF_TEMPERATURE_UNIT] in (BARE_TEMP_CELSIUS, TEMP_CELSIUS): + temperature_unit = TEMP_CELSIUS + + config = {"temperature_unit": temperature_unit} if not conf[CONF_AUTO_CONFIGURE]: # With elkm1-lib==0.7.16 and later auto configure is available diff --git a/homeassistant/components/elkm1/climate.py b/homeassistant/components/elkm1/climate.py index 3c5c70b2bd0..baaf3d44eb2 100644 --- a/homeassistant/components/elkm1/climate.py +++ b/homeassistant/components/elkm1/climate.py @@ -14,7 +14,7 @@ from homeassistant.components.climate.const import ( SUPPORT_FAN_MODE, SUPPORT_TARGET_TEMPERATURE_RANGE, ) -from homeassistant.const import PRECISION_WHOLE, STATE_ON, TEMP_CELSIUS, TEMP_FAHRENHEIT +from homeassistant.const import PRECISION_WHOLE, STATE_ON from . import ElkEntity, create_elk_entities from .const import DOMAIN @@ -55,7 +55,7 @@ class ElkThermostat(ElkEntity, ClimateDevice): @property def temperature_unit(self): """Return the temperature unit.""" - return TEMP_FAHRENHEIT if self._temperature_unit == "F" else TEMP_CELSIUS + return self._temperature_unit @property def current_temperature(self): diff --git a/homeassistant/components/elkm1/config_flow.py b/homeassistant/components/elkm1/config_flow.py index c96e6e549c0..419e4df7552 100644 --- a/homeassistant/components/elkm1/config_flow.py +++ b/homeassistant/components/elkm1/config_flow.py @@ -13,6 +13,8 @@ from homeassistant.const import ( CONF_PROTOCOL, CONF_TEMPERATURE_UNIT, CONF_USERNAME, + TEMP_CELSIUS, + TEMP_FAHRENHEIT, ) from homeassistant.util import slugify @@ -33,7 +35,9 @@ DATA_SCHEMA = vol.Schema( vol.Optional(CONF_USERNAME, default=""): str, vol.Optional(CONF_PASSWORD, default=""): str, vol.Optional(CONF_PREFIX, default=""): str, - vol.Optional(CONF_TEMPERATURE_UNIT, default="F"): vol.In(["F", "C"]), + vol.Optional(CONF_TEMPERATURE_UNIT, default=TEMP_FAHRENHEIT): vol.In( + [TEMP_FAHRENHEIT, TEMP_CELSIUS] + ), } ) diff --git a/homeassistant/components/elkm1/const.py b/homeassistant/components/elkm1/const.py index 21cd6aaa97a..27b6445a4c1 100644 --- a/homeassistant/components/elkm1/const.py +++ b/homeassistant/components/elkm1/const.py @@ -18,6 +18,9 @@ CONF_ZONE = "zone" CONF_PREFIX = "prefix" +BARE_TEMP_FAHRENHEIT = "F" +BARE_TEMP_CELSIUS = "C" + ELK_ELEMENTS = { CONF_AREA: Max.AREAS.value, CONF_COUNTER: Max.COUNTERS.value, diff --git a/tests/components/elkm1/test_config_flow.py b/tests/components/elkm1/test_config_flow.py index 02e3fd7fce9..2f701a2e146 100644 --- a/tests/components/elkm1/test_config_flow.py +++ b/tests/components/elkm1/test_config_flow.py @@ -39,7 +39,7 @@ async def test_form_user_with_secure_elk(hass): "address": "1.2.3.4", "username": "test-username", "password": "test-password", - "temperature_unit": "F", + "temperature_unit": "°F", "prefix": "", }, ) @@ -51,7 +51,7 @@ async def test_form_user_with_secure_elk(hass): "host": "elks://1.2.3.4", "password": "test-password", "prefix": "", - "temperature_unit": "F", + "temperature_unit": "°F", "username": "test-username", } await hass.async_block_till_done() @@ -82,7 +82,7 @@ async def test_form_user_with_non_secure_elk(hass): { "protocol": "non-secure", "address": "1.2.3.4", - "temperature_unit": "F", + "temperature_unit": "°F", "prefix": "guest_house", }, ) @@ -95,7 +95,7 @@ async def test_form_user_with_non_secure_elk(hass): "prefix": "guest_house", "username": "", "password": "", - "temperature_unit": "F", + "temperature_unit": "°F", } await hass.async_block_till_done() assert len(mock_setup.mock_calls) == 1 @@ -125,7 +125,7 @@ async def test_form_user_with_serial_elk(hass): { "protocol": "serial", "address": "/dev/ttyS0:115200", - "temperature_unit": "F", + "temperature_unit": "°C", "prefix": "", }, ) @@ -138,7 +138,7 @@ async def test_form_user_with_serial_elk(hass): "prefix": "", "username": "", "password": "", - "temperature_unit": "F", + "temperature_unit": "°C", } await hass.async_block_till_done() assert len(mock_setup.mock_calls) == 1 @@ -166,7 +166,7 @@ async def test_form_cannot_connect(hass): "address": "1.2.3.4", "username": "test-username", "password": "test-password", - "temperature_unit": "F", + "temperature_unit": "°F", "prefix": "", }, ) @@ -193,7 +193,7 @@ async def test_form_invalid_auth(hass): "address": "1.2.3.4", "username": "test-username", "password": "test-password", - "temperature_unit": "F", + "temperature_unit": "°F", "prefix": "", }, )