Fix validation of cost entities for energy dashboard (#56219)
This commit is contained in:
parent
351ef0ab44
commit
f77e93ceeb
2 changed files with 50 additions and 32 deletions
|
@ -192,27 +192,13 @@ def _async_validate_cost_stat(
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
state = hass.states.get(stat_id)
|
||||||
@callback
|
|
||||||
def _async_validate_cost_entity(
|
|
||||||
hass: HomeAssistant, entity_id: str, result: list[ValidationIssue]
|
|
||||||
) -> None:
|
|
||||||
"""Validate that the cost entity is correct."""
|
|
||||||
if not recorder.is_entity_recorded(hass, entity_id):
|
|
||||||
result.append(
|
|
||||||
ValidationIssue(
|
|
||||||
"recorder_untracked",
|
|
||||||
entity_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
|
||||||
|
|
||||||
if state is None:
|
if state is None:
|
||||||
result.append(
|
result.append(
|
||||||
ValidationIssue(
|
ValidationIssue(
|
||||||
"entity_not_defined",
|
"entity_not_defined",
|
||||||
entity_id,
|
stat_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
@ -227,7 +213,21 @@ def _async_validate_cost_entity(
|
||||||
if state_class not in supported_state_classes:
|
if state_class not in supported_state_classes:
|
||||||
result.append(
|
result.append(
|
||||||
ValidationIssue(
|
ValidationIssue(
|
||||||
"entity_unexpected_state_class_total_increasing", entity_id, state_class
|
"entity_unexpected_state_class_total_increasing", stat_id, state_class
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _async_validate_auto_generated_cost_entity(
|
||||||
|
hass: HomeAssistant, entity_id: str, result: list[ValidationIssue]
|
||||||
|
) -> None:
|
||||||
|
"""Validate that the auto generated cost entity is correct."""
|
||||||
|
if not recorder.is_entity_recorded(hass, entity_id):
|
||||||
|
result.append(
|
||||||
|
ValidationIssue(
|
||||||
|
"recorder_untracked",
|
||||||
|
entity_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -259,11 +259,12 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation:
|
||||||
if flow.get("stat_cost") is not None:
|
if flow.get("stat_cost") is not None:
|
||||||
_async_validate_cost_stat(hass, flow["stat_cost"], source_result)
|
_async_validate_cost_stat(hass, flow["stat_cost"], source_result)
|
||||||
|
|
||||||
elif flow.get("entity_energy_price") is not None:
|
else:
|
||||||
|
if flow.get("entity_energy_price") is not None:
|
||||||
_async_validate_price_entity(
|
_async_validate_price_entity(
|
||||||
hass, flow["entity_energy_price"], source_result
|
hass, flow["entity_energy_price"], source_result
|
||||||
)
|
)
|
||||||
_async_validate_cost_entity(
|
_async_validate_auto_generated_cost_entity(
|
||||||
hass,
|
hass,
|
||||||
hass.data[DOMAIN]["cost_sensors"][flow["stat_energy_from"]],
|
hass.data[DOMAIN]["cost_sensors"][flow["stat_energy_from"]],
|
||||||
source_result,
|
source_result,
|
||||||
|
@ -284,11 +285,12 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation:
|
||||||
hass, flow["stat_compensation"], source_result
|
hass, flow["stat_compensation"], source_result
|
||||||
)
|
)
|
||||||
|
|
||||||
elif flow.get("entity_energy_price") is not None:
|
else:
|
||||||
|
if flow.get("entity_energy_price") is not None:
|
||||||
_async_validate_price_entity(
|
_async_validate_price_entity(
|
||||||
hass, flow["entity_energy_price"], source_result
|
hass, flow["entity_energy_price"], source_result
|
||||||
)
|
)
|
||||||
_async_validate_cost_entity(
|
_async_validate_auto_generated_cost_entity(
|
||||||
hass,
|
hass,
|
||||||
hass.data[DOMAIN]["cost_sensors"][flow["stat_energy_to"]],
|
hass.data[DOMAIN]["cost_sensors"][flow["stat_energy_to"]],
|
||||||
source_result,
|
source_result,
|
||||||
|
@ -307,11 +309,12 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation:
|
||||||
if source.get("stat_cost") is not None:
|
if source.get("stat_cost") is not None:
|
||||||
_async_validate_cost_stat(hass, source["stat_cost"], source_result)
|
_async_validate_cost_stat(hass, source["stat_cost"], source_result)
|
||||||
|
|
||||||
elif source.get("entity_energy_price") is not None:
|
else:
|
||||||
|
if source.get("entity_energy_price") is not None:
|
||||||
_async_validate_price_entity(
|
_async_validate_price_entity(
|
||||||
hass, source["entity_energy_price"], source_result
|
hass, source["entity_energy_price"], source_result
|
||||||
)
|
)
|
||||||
_async_validate_cost_entity(
|
_async_validate_auto_generated_cost_entity(
|
||||||
hass,
|
hass,
|
||||||
hass.data[DOMAIN]["cost_sensors"][source["stat_energy_from"]],
|
hass.data[DOMAIN]["cost_sensors"][source["stat_energy_from"]],
|
||||||
source_result,
|
source_result,
|
||||||
|
|
|
@ -331,6 +331,11 @@ async def test_validation_grid(hass, mock_energy_manager, mock_is_entity_recorde
|
||||||
"identifier": "sensor.grid_cost_1",
|
"identifier": "sensor.grid_cost_1",
|
||||||
"value": None,
|
"value": None,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "entity_not_defined",
|
||||||
|
"identifier": "sensor.grid_cost_1",
|
||||||
|
"value": None,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "entity_unexpected_unit_energy",
|
"type": "entity_unexpected_unit_energy",
|
||||||
"identifier": "sensor.grid_production_1",
|
"identifier": "sensor.grid_production_1",
|
||||||
|
@ -341,6 +346,11 @@ async def test_validation_grid(hass, mock_energy_manager, mock_is_entity_recorde
|
||||||
"identifier": "sensor.grid_compensation_1",
|
"identifier": "sensor.grid_compensation_1",
|
||||||
"value": None,
|
"value": None,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "entity_not_defined",
|
||||||
|
"identifier": "sensor.grid_compensation_1",
|
||||||
|
"value": None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
"device_consumption": [],
|
"device_consumption": [],
|
||||||
|
@ -558,6 +568,11 @@ async def test_validation_gas(hass, mock_energy_manager, mock_is_entity_recorded
|
||||||
"identifier": "sensor.gas_cost_1",
|
"identifier": "sensor.gas_cost_1",
|
||||||
"value": None,
|
"value": None,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "entity_not_defined",
|
||||||
|
"identifier": "sensor.gas_cost_1",
|
||||||
|
"value": None,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue