This commit is contained in:
G Johansson 2024-07-12 15:05:09 +00:00
parent d73e12df93
commit ebb450db48
4 changed files with 31 additions and 9 deletions

View File

@ -159,7 +159,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Compensation from a config entry."""
await create_compensation_data(hass, entry.entry_id, dict(entry.options), True)
config = dict(entry.options)
data_points = config[CONF_DATAPOINTS]
new_data_points = []
for data_point in data_points:
values = data_point.split(",", maxsplit=1)
new_data_points.append([float(values[0]), float(values[1])])
config[CONF_DATAPOINTS] = new_data_points
await create_compensation_data(hass, entry.entry_id, config, True)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))

View File

@ -76,6 +76,20 @@ async def get_options_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
)
def _is_valid_data_points(check_data_points: list[str]) -> bool:
"""Validate data points."""
for data_point in check_data_points:
if data_point.find(",") > 0:
values = data_point.split(",", maxsplit=1)
for value in values:
try:
float(value)
except ValueError:
return False
return True
return False
async def validate_options(
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
) -> dict[str, Any]:
@ -84,9 +98,8 @@ async def validate_options(
user_input[CONF_PRECISION] = int(user_input[CONF_PRECISION])
user_input[CONF_DEGREE] = int(user_input[CONF_DEGREE])
for datapoint in user_input[CONF_DATAPOINTS]:
if not isinstance(datapoint, list):
raise SchemaFlowError("incorrect_datapoints")
if not _is_valid_data_points(user_input[CONF_DATAPOINTS]):
raise SchemaFlowError("incorrect_datapoints")
if len(user_input[CONF_DATAPOINTS]) <= user_input[CONF_DEGREE]:
raise SchemaFlowError("not_enough_datapoints")

View File

@ -12,6 +12,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
CONF_ATTRIBUTE,
CONF_ENTITY_ID,
CONF_MAXIMUM,
CONF_MINIMUM,
CONF_SOURCE,
@ -90,14 +91,14 @@ async def async_setup_entry(
compensation = entry.entry_id
conf: dict[str, Any] = hass.data[DATA_COMPENSATION][compensation]
source: str = conf[CONF_SOURCE]
source: str = conf[CONF_ENTITY_ID]
attribute: str | None = conf.get(CONF_ATTRIBUTE)
name = entry.title
async_add_entities(
[
CompensationSensor(
conf.get(CONF_UNIQUE_ID),
entry.entry_id,
name,
source,
attribute,

View File

@ -4,8 +4,8 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
},
"error": {
"incorrect_datapoints": "Datapoints needs to be provided in list-format, ex. '[1.0, 0.0]'.",
"not_enough_datapoints": "The number of datapoints needs to be less or equal to configured degree."
"incorrect_datapoints": "Datapoints needs to be provided in the right format, ex. '1.0, 0.0'.",
"not_enough_datapoints": "The number of datapoints needs to be more than the configured degree."
},
"step": {
"user": {
@ -31,7 +31,7 @@
"unit_of_measurement": "Unit of measurement"
},
"data_description": {
"data_points": "The collection of data point conversions with the format '[uncompensated_value, compensated_value]'",
"data_points": "The collection of data point conversions with the format 'uncompensated_value, compensated_value', ex. '1.0, 0.0'",
"attribute": "Attribute from the source to monitor/compensate.",
"upper_limit": "Enables an upper limit for the sensor. The upper limit is defined by the data collections (data_points) greatest uncompensated value.",
"lower_limit": "Enables a lower limit for the sensor. The lower limit is defined by the data collections (data_points) lowest uncompensated value.",