diff --git a/homeassistant/components/energy/validate.py b/homeassistant/components/energy/validate.py index 29c5ae88b48..d03883d046b 100644 --- a/homeassistant/components/energy/validate.py +++ b/homeassistant/components/energy/validate.py @@ -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"]], diff --git a/tests/components/energy/test_validate.py b/tests/components/energy/test_validate.py index d566eb51b28..1dd38047209 100644 --- a/tests/components/energy/test_validate.py +++ b/tests/components/energy/test_validate.py @@ -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": "m³", + "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": [], + }