Make utility_meter tariffs a list (#68967)

This commit is contained in:
Erik Montnemery 2022-03-31 13:57:26 +02:00 committed by GitHub
parent 3c478c312a
commit 400943ce99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 38 deletions

View File

@ -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": {}}
),

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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",

View File

@ -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": [],
},
),
),