Optimizing and controlling media flows in heat pump systems