From 400943ce998f81ab4abb198429c1654b998ce173 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 31 Mar 2022 13:57:26 +0200 Subject: [PATCH] Make utility_meter tariffs a list (#68967) --- .../components/utility_meter/config_flow.py | 11 ++++------- homeassistant/components/utility_meter/select.py | 7 +------ homeassistant/components/utility_meter/sensor.py | 7 +------ .../components/utility_meter/test_config_flow.py | 14 +++++++------- tests/components/utility_meter/test_init.py | 8 ++++---- tests/components/utility_meter/test_sensor.py | 16 ++++++++-------- 6 files changed, 25 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/utility_meter/config_flow.py b/homeassistant/components/utility_meter/config_flow.py index 555ae7eb46d..ed12b3038b6 100644 --- a/homeassistant/components/utility_meter/config_flow.py +++ b/homeassistant/components/utility_meter/config_flow.py @@ -48,13 +48,8 @@ METER_TYPES = [ def _validate_config(data: Any) -> Any: """Validate config.""" - tariffs: list[str] - if not data[CONF_TARIFFS]: - tariffs = [] - else: - tariffs = data[CONF_TARIFFS].split(",") try: - vol.Unique()(tariffs) + vol.Unique()(data[CONF_TARIFFS]) except vol.Invalid as exc: raise SchemaFlowError("tariffs_not_unique") from exc @@ -88,7 +83,9 @@ CONFIG_SCHEMA = vol.Schema( } } ), - vol.Optional(CONF_TARIFFS): selector.selector({"text": {}}), + vol.Required(CONF_TARIFFS, default=[]): selector.selector( + {"select": {"options": [], "custom_value": True, "multiple": True}} + ), vol.Required(CONF_METER_NET_CONSUMPTION, default=False): selector.selector( {"boolean": {}} ), diff --git a/homeassistant/components/utility_meter/select.py b/homeassistant/components/utility_meter/select.py index e47f0626f6e..1f39b7f7c16 100644 --- a/homeassistant/components/utility_meter/select.py +++ b/homeassistant/components/utility_meter/select.py @@ -42,12 +42,7 @@ async def async_setup_entry( ) -> None: """Initialize Utility Meter config entry.""" name = config_entry.title - - # Remove when frontend list selector is available - if not config_entry.options.get(CONF_TARIFFS): - tariffs = [] - else: - tariffs = config_entry.options[CONF_TARIFFS].split(",") + tariffs = config_entry.options[CONF_TARIFFS] legacy_add_entities = None unique_id = config_entry.entry_id diff --git a/homeassistant/components/utility_meter/sensor.py b/homeassistant/components/utility_meter/sensor.py index 7800c5035fb..c3d2be63a4b 100644 --- a/homeassistant/components/utility_meter/sensor.py +++ b/homeassistant/components/utility_meter/sensor.py @@ -119,12 +119,7 @@ async def async_setup_entry( tariff_entity = hass.data[DATA_UTILITY][entry_id][CONF_TARIFF_ENTITY] meters = [] - - # Remove when frontend list selector is available - if not config_entry.options.get(CONF_TARIFFS): - tariffs = [] - else: - tariffs = config_entry.options[CONF_TARIFFS].split(",") + tariffs = config_entry.options[CONF_TARIFFS] if not tariffs: # Add single sensor, not gated by a tariff selector diff --git a/tests/components/utility_meter/test_config_flow.py b/tests/components/utility_meter/test_config_flow.py index dd2d99617c6..53f9d814f2f 100644 --- a/tests/components/utility_meter/test_config_flow.py +++ b/tests/components/utility_meter/test_config_flow.py @@ -33,7 +33,7 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: "name": "Electricity meter", "offset": 0, "source": input_sensor_entity_id, - "tariffs": "", + "tariffs": [], }, ) await hass.async_block_till_done() @@ -48,7 +48,7 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: "net_consumption": False, "offset": 0, "source": input_sensor_entity_id, - "tariffs": "", + "tariffs": [], } assert len(mock_setup_entry.mock_calls) == 1 @@ -61,7 +61,7 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: "net_consumption": False, "offset": 0, "source": input_sensor_entity_id, - "tariffs": "", + "tariffs": [], } assert config_entry.title == "Electricity meter" @@ -83,7 +83,7 @@ async def test_tariffs(hass: HomeAssistant) -> None: "name": "Electricity meter", "offset": 0, "source": input_sensor_entity_id, - "tariffs": "cat,dog,horse,cow", + "tariffs": ["cat", "dog", "horse", "cow"], }, ) await hass.async_block_till_done() @@ -98,7 +98,7 @@ async def test_tariffs(hass: HomeAssistant) -> None: "net_consumption": False, "offset": 0, "source": input_sensor_entity_id, - "tariffs": "cat,dog,horse,cow", + "tariffs": ["cat", "dog", "horse", "cow"], } config_entry = hass.config_entries.async_entries(DOMAIN)[0] @@ -110,7 +110,7 @@ async def test_tariffs(hass: HomeAssistant) -> None: "net_consumption": False, "offset": 0, "source": input_sensor_entity_id, - "tariffs": "cat,dog,horse,cow", + "tariffs": ["cat", "dog", "horse", "cow"], } assert config_entry.title == "Electricity meter" @@ -127,7 +127,7 @@ async def test_tariffs(hass: HomeAssistant) -> None: "name": "Electricity meter", "offset": 0, "source": input_sensor_entity_id, - "tariffs": "cat,cat,cat,cat", + "tariffs": ["cat", "cat", "cat", "cat"], }, ) await hass.async_block_till_done() diff --git a/tests/components/utility_meter/test_init.py b/tests/components/utility_meter/test_init.py index 925ff00f323..853fd827f0a 100644 --- a/tests/components/utility_meter/test_init.py +++ b/tests/components/utility_meter/test_init.py @@ -186,7 +186,7 @@ async def test_services_config_entry(hass): "net_consumption": False, "offset": 0, "source": "sensor.energy", - "tariffs": "peak,offpeak", + "tariffs": ["peak", "offpeak"], }, title="Energy bill", ) @@ -202,7 +202,7 @@ async def test_services_config_entry(hass): "net_consumption": False, "offset": 0, "source": "sensor.energy", - "tariffs": "peak,offpeak", + "tariffs": ["peak", "offpeak"], }, title="Energy bill2", ) @@ -469,11 +469,11 @@ async def test_legacy_support(hass): "tariffs,expected_entities", ( ( - "", + [], ["sensor.electricity_meter"], ), ( - "high,low", + ["high", "low"], [ "sensor.electricity_meter_low", "sensor.electricity_meter_high", diff --git a/tests/components/utility_meter/test_sensor.py b/tests/components/utility_meter/test_sensor.py index 04610f6c2f4..1b8328f5a62 100644 --- a/tests/components/utility_meter/test_sensor.py +++ b/tests/components/utility_meter/test_sensor.py @@ -77,7 +77,7 @@ def alter_time(retval): "net_consumption": False, "offset": 0, "source": "sensor.energy", - "tariffs": "onpeak,midpeak,offpeak", + "tariffs": ["onpeak", "midpeak", "offpeak"], }, ), ), @@ -249,7 +249,7 @@ async def test_state(hass, yaml_config, config_entry_config): "net_consumption": False, "offset": 0, "source": "sensor.energy", - "tariffs": "onpeak,midpeak,offpeak", + "tariffs": ["onpeak", "midpeak", "offpeak"], }, ), ), @@ -327,7 +327,7 @@ async def test_init(hass, yaml_config, config_entry_config): "net_consumption": True, "offset": 0, "source": "sensor.energy", - "tariffs": "", + "tariffs": [], }, { "cycle": "none", @@ -336,7 +336,7 @@ async def test_init(hass, yaml_config, config_entry_config): "net_consumption": False, "offset": 0, "source": "sensor.gas", - "tariffs": "", + "tariffs": [], }, ], ), @@ -411,7 +411,7 @@ async def test_device_class(hass, yaml_config, config_entry_configs): "net_consumption": False, "offset": 0, "source": "sensor.energy", - "tariffs": "onpeak,midpeak,offpeak", + "tariffs": ["onpeak", "midpeak", "offpeak"], }, ), ), @@ -514,7 +514,7 @@ async def test_restore_state(hass, yaml_config, config_entry_config): "net_consumption": True, "offset": 0, "source": "sensor.energy", - "tariffs": "", + "tariffs": [], }, ), ), @@ -582,7 +582,7 @@ async def test_net_consumption(hass, yaml_config, config_entry_config): "net_consumption": False, "offset": 0, "source": "sensor.energy", - "tariffs": "", + "tariffs": [], }, ), ), @@ -650,7 +650,7 @@ async def test_non_net_consumption(hass, yaml_config, config_entry_config): "net_consumption": False, "offset": 0, "source": "sensor.energy", - "tariffs": "", + "tariffs": [], }, ), ),