Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions homeassistant/components/elkm1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
CONF_PASSWORD,
CONF_TEMPERATURE_UNIT,
CONF_USERNAME,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
Expand All @@ -23,6 +25,8 @@
from homeassistant.helpers.typing import ConfigType

from .const import (
BARE_TEMP_CELSIUS,
BARE_TEMP_FAHRENHEIT,
CONF_AREA,
CONF_AUTO_CONFIGURE,
CONF_COUNTER,
Expand Down Expand Up @@ -119,7 +123,10 @@ def _has_all_unique_prefixes(value):
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,
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to implement a config entry migration handler to update the config entry data to the correct state before the integration is set up.

See eg:

async def async_migrate_entry(hass: HomeAssistantType, config_entry: ConfigEntry):
"""Migrate config entry to new version."""
if config_entry.version == 1:
options = config_entry.options
recipient = options.get(CONF_RECIPIENT)
if isinstance(recipient, str):
options[CONF_RECIPIENT] = [x.strip() for x in recipient.split(",")]
config_entry.version = 2
hass.config_entries.async_update_entry(config_entry, options=options)
_LOGGER.info("Migrated config entry to version %d", config_entry.version)
return True

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Will look at cleaning this up once everything settles a bit more.


config = {"temperature_unit": temperature_unit}

if not conf[CONF_AUTO_CONFIGURE]:
# With elkm1-lib==0.7.16 and later auto configure is available
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/elkm1/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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
Expand Down Expand Up @@ -55,7 +55,7 @@ def supported_features(self):
@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):
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/elkm1/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
CONF_PROTOCOL,
CONF_TEMPERATURE_UNIT,
CONF_USERNAME,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.util import slugify

Expand All @@ -33,7 +35,9 @@
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]
),
}
)

Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/elkm1/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
CONF_PREFIX = "prefix"


BARE_TEMP_FAHRENHEIT = "F"
BARE_TEMP_CELSIUS = "C"

ELK_ELEMENTS = {
CONF_AREA: Max.AREAS.value,
CONF_COUNTER: Max.COUNTERS.value,
Expand Down
16 changes: 8 additions & 8 deletions tests/components/elkm1/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
},
)
Expand All @@ -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()
Expand Down Expand Up @@ -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",
},
)
Expand All @@ -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
Expand Down Expand Up @@ -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": "",
},
)
Expand All @@ -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
Expand Down Expand Up @@ -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": "",
},
)
Expand All @@ -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": "",
},
)
Expand Down