Fix Huawei LTE SMS recipient setting from options UI (#31117)

* Fix Huawei LTE SMS recipient setting from options UI

Refs https://github.com/home-assistant/home-assistant/issues/30827

* Use core interfaces in tests

* ...more
This commit is contained in:
Ville Skyttä authored and GitHub committed 2020-01-25 13:09:43 +02:00
1 parent a007835293
commit 98ac84349c
3 files changed
+92 -35

No files matched your search

@@ -506,6 +506,19 @@ async def async_signal_options_update(
async_dispatcher_send(hass, UPDATE_OPTIONS_SIGNAL, config_entry)
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[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
@attr.s
class HuaweiLteBaseEntity(Entity):
"""Huawei LTE entity base class."""
@@ -40,7 +40,7 @@ _LOGGER = logging.getLogger(__name__)
class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle Huawei LTE config flow."""
VERSION = 1
VERSION = 2
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
@staticmethod
@@ -247,9 +247,16 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
async def async_step_init(self, user_input=None):
"""Handle options flow."""
# Recipients are persisted as a list, but handled as comma separated string in UI
if user_input is not None:
# Preserve existing options, for example *_from_yaml markers
data = {**self.config_entry.options, **user_input}
if not isinstance(data[CONF_RECIPIENT], list):
data[CONF_RECIPIENT] = [
x.strip() for x in data[CONF_RECIPIENT].split(",")
]
return self.async_create_entry(title="", data=data)
data_schema = vol.Schema(
@@ -262,7 +269,9 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
): str,
vol.Optional(
CONF_RECIPIENT,
default=self.config_entry.options.get(CONF_RECIPIENT, ""),
default=", ".join(
self.config_entry.options.get(CONF_RECIPIENT, [])
),
): str,
}
)