Fix energy validation when not tracking costs ()

pull/56806/head
Franck Nijhof 2021-09-29 13:24:34 +02:00 committed by GitHub
parent d3df6f26f9
commit 41e5f05d99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 15 deletions
homeassistant/components/energy
tests/components/energy

View File

@ -272,12 +272,15 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation:
if flow.get("stat_cost") is not None:
_async_validate_cost_stat(hass, flow["stat_cost"], source_result)
elif flow.get("entity_energy_price") is not None:
_async_validate_price_entity(
hass, flow["entity_energy_price"], source_result
)
else:
if flow.get("entity_energy_price") is not None:
_async_validate_price_entity(
hass, flow["entity_energy_price"], source_result
)
if (
flow.get("entity_energy_price") is not None
or flow.get("number_energy_price") is not None
):
_async_validate_auto_generated_cost_entity(
hass,
hass.data[DOMAIN]["cost_sensors"][flow["stat_energy_from"]],
@ -298,12 +301,15 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation:
_async_validate_cost_stat(
hass, flow["stat_compensation"], source_result
)
elif flow.get("entity_energy_price") is not None:
_async_validate_price_entity(
hass, flow["entity_energy_price"], source_result
)
else:
if flow.get("entity_energy_price") is not None:
_async_validate_price_entity(
hass, flow["entity_energy_price"], source_result
)
if (
flow.get("entity_energy_price") is not None
or flow.get("number_energy_price") is not None
):
_async_validate_auto_generated_cost_entity(
hass,
hass.data[DOMAIN]["cost_sensors"][flow["stat_energy_to"]],
@ -322,12 +328,15 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation:
if source.get("stat_cost") is not None:
_async_validate_cost_stat(hass, source["stat_cost"], source_result)
elif source.get("entity_energy_price") is not None:
_async_validate_price_entity(
hass, source["entity_energy_price"], source_result
)
else:
if source.get("entity_energy_price") is not None:
_async_validate_price_entity(
hass, source["entity_energy_price"], source_result
)
if (
source.get("entity_energy_price") is not None
or source.get("number_energy_price") is not None
):
_async_validate_auto_generated_cost_entity(
hass,
hass.data[DOMAIN]["cost_sensors"][source["stat_energy_from"]],

View File

@ -625,3 +625,85 @@ async def test_validation_gas(hass, mock_energy_manager, mock_is_entity_recorded
],
"device_consumption": [],
}
async def test_validation_gas_no_costs_tracking(
hass, mock_energy_manager, mock_is_entity_recorded
):
"""Test validating gas with sensors without cost tracking."""
await mock_energy_manager.async_update(
{
"energy_sources": [
{
"type": "gas",
"stat_energy_from": "sensor.gas_consumption_1",
"stat_cost": None,
"entity_energy_from": None,
"entity_energy_price": None,
"number_energy_price": None,
},
]
}
)
hass.states.async_set(
"sensor.gas_consumption_1",
"10.10",
{
"device_class": "gas",
"unit_of_measurement": "",
"state_class": "total_increasing",
},
)
assert (await validate.async_validate(hass)).as_dict() == {
"energy_sources": [[]],
"device_consumption": [],
}
async def test_validation_grid_no_costs_tracking(
hass, mock_energy_manager, mock_is_entity_recorded
):
"""Test validating grid with sensors for energy without cost tracking."""
await mock_energy_manager.async_update(
{
"energy_sources": [
{
"type": "grid",
"flow_from": [
{
"stat_energy_from": "sensor.grid_energy",
"stat_cost": None,
"entity_energy_from": "sensor.grid_energy",
"entity_energy_price": None,
"number_energy_price": None,
},
],
"flow_to": [
{
"stat_energy_to": "sensor.grid_energy",
"stat_cost": None,
"entity_energy_to": "sensor.grid_energy",
"entity_energy_price": None,
"number_energy_price": None,
},
],
"cost_adjustment_day": 0.0,
}
]
}
)
hass.states.async_set(
"sensor.grid_energy",
"10.10",
{
"device_class": "energy",
"unit_of_measurement": "kWh",
"state_class": "total_increasing",
},
)
assert (await validate.async_validate(hass)).as_dict() == {
"energy_sources": [[]],
"device_consumption": [],
}